質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2727閲覧

Swift ライトの設定に関して

Susanoo2442

総合スコア153

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/01/16 11:00

iPhone 5s
Xcode8
Swift3.0
ios 最新

Swiftでボタンを押したら
iPhoneに装備しているライトをオンにするコードを書いているのですが

どうもうまくいきません。

まずは、コードを見て頂きたいのですが

```Swift

func Grid(sender: UIButton){

let aaa = AVCaptureDevice() if (aaa.hasTorch){ if (aaa.isTorchAvailable){ do{ try aaa.lockForConfiguration() if (aaa.isTorchActive){ aaa.torchMode = AVCaptureTorchMode.off } else { aaa.torchMode = AVCaptureTorchMode.on } aaa.unlockForConfiguration() } catch{ let alertController = UIAlertController(title: "Cheak", message: "ERROR !!", preferredStyle: .alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(defaultAction) present(alertController, animated: true, completion: nil) } } else { let alertController = UIAlertController(title: "Cheak", message: "This iPhone is not use Torch !!", preferredStyle: .alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(defaultAction) present(alertController, animated: true, completion: nil) } } else { let alertController = UIAlertController(title: "Cheak", message: "This iPhone is not Torch !!", preferredStyle: .alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(defaultAction) present(alertController, animated: true, completion: nil) } }
この部分なのですが AVFoundationのAVCaptureDeviceクラスを使っております。 そして、このGridという関数ですが こちらは、ボタンの関数となっていましてaddSubviewで追加したものになります。 こちらのGridというボタンを押した時にクラッシュしてしまいます。 きちんとエラー処理もしているはずなのですが 何が原因なのでしょうか? 詳しい方ご教示して頂ければと思います。 よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

初期化で失敗しているので、以下様に初期化部分を変更してみてください。

swift

1let aaa = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

変更後のコード

swift

1func Grid(sender: UIButton){ 2 3 if let aaa = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) { 4 5 if (aaa.hasTorch){ 6 7 if (aaa.isTorchAvailable){ 8 9 do{ 10 try aaa.lockForConfiguration() 11 12 if (aaa.isTorchActive){ 13 14 aaa.torchMode = AVCaptureTorchMode.off 15 } else { 16 aaa.torchMode = AVCaptureTorchMode.on 17 } 18 19 aaa.unlockForConfiguration() 20 21 } catch{ 22 let alertController = UIAlertController(title: "Cheak", message: "ERROR !!", preferredStyle: .alert) 23 24 let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) 25 alertController.addAction(defaultAction) 26 present(alertController, animated: true, completion: nil) 27 } 28 } else { 29 let alertController = UIAlertController(title: "Cheak", message: "This iPhone is not use Torch !!", preferredStyle: .alert) 30 31 let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) 32 alertController.addAction(defaultAction) 33 present(alertController, animated: true, completion: nil) 34 } 35 } else { 36 let alertController = UIAlertController(title: "Cheak", message: "This iPhone is not Torch !!", preferredStyle: .alert) 37 38 let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) 39 alertController.addAction(defaultAction) 40 present(alertController, animated: true, completion: nil) 41 } 42 } 43} 44

投稿2017/01/16 11:57

編集2017/01/16 11:59
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Susanoo2442

2017/01/16 13:23

無事できました!ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問