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

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

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

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

Q&A

1回答

2058閲覧

カメラアクセスと写真のアップロードができない

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

0グッド

0クリップ

投稿2017/02/26 09:16

作ったアプリでカメラアクセスと写真のアップロードができません。
Xcodeから自分のiPhoneでエミュレータを起動したのですが
Camera access ボタンとPhoto upload ボタンは自分のiPhoneで表示されたのですが
それらは押せるだけで本当の機能ができません。
ボタンはコントローラにくっついています。
OSは10.12(Sierra)でボタンを押した時に何もエラーは出ません。
コントローラには

import UIKit class SendController:UIViewController, UINavigationControllerDelegate,UIImagePickerControllerDelegate{ let ButtonCamera = 0 let ButtomRead = 1 let ButtonWrite = 2 var imageView:UIImageView! var btnCamera:UIButton! var btnRead:UIButton! var btnWrite:UIButton! override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(btnCamera) self.view.addSubview(btnRead) self.view.addSubview(imageView) } func onClick(sender:UIButton){ if sender.tag == ButtonCamera { openPicker(sourceType: UIImagePickerControllerSourceType.camera) }else if sender.tag == ButtomRead { openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary) } } func showAlert(title: String?, text: String?) { let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) present(alert, animated: true, completion: nil) } func openPicker(sourceType:UIImagePickerControllerSourceType){ if !UIImagePickerController.isSourceTypeAvailable(sourceType){ showAlert(title: nil, text: "UnableUse") return } let picker = UIImagePickerController() picker.sourceType = sourceType picker.delegate = self present(picker, animated: true, completion: nil) } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let image = info[UIImagePickerControllerOriginalImage]as! UIImage imageView.image = image picker.presentingViewController?.dismiss(animated: true,completion:nil) } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.presentingViewController?.dismiss(animated: true, completion: nil) } }

と書きました。
どう直せばいいのでしょうか?

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

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

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

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

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

guest

回答1

0

ボタン等の部品が生成されていないのではないでしょうか?

swift

1import UIKit 2 3class ViewController:UIViewController, 4UINavigationControllerDelegate,UIImagePickerControllerDelegate{ 5 6 let ButtonCamera = 0 7 let ButtomRead = 1 8 let ButtonWrite = 2 9 10 var imageView:UIImageView = UIImageView() 11 12 var btnCamera:UIButton = UIButton(type: .custom) 13 var btnRead:UIButton = UIButton(type: .custom) 14 var btnWrite:UIButton = UIButton(type: .custom) 15 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 imageView.frame = CGRect(x: 150, y: 100, width: 200, height: 200) 21 imageView.contentMode = .scaleAspectFit 22 view.addSubview(imageView) 23 24 btnCamera.frame = CGRect(x: 0, y: 100, width: 100, height: 100) 25 btnCamera.setTitle("Camera", for: .normal) 26 btnCamera.tag = ButtonCamera 27 btnCamera.addTarget(self, action: #selector(self.onClick(sender:)), for: .touchUpInside) 28 btnCamera.backgroundColor = UIColor.green 29 self.view.addSubview(btnCamera) 30 31 btnRead.frame = CGRect(x: 0, y: 200, width: 100, height: 100) 32 btnRead.setTitle("Read", for: .normal) 33 btnRead.tag = ButtomRead 34 btnRead.addTarget(self, action: #selector(self.onClick(sender:)), for: .touchUpInside) 35 btnRead.backgroundColor = UIColor.red 36 self.view.addSubview(btnRead) 37 38 btnWrite.frame = CGRect(x: 0, y: 300, width: 100, height: 100) 39 btnWrite.setTitle("Write", for: .normal) 40 btnWrite.tag = ButtonWrite 41 btnWrite.addTarget(self, action: #selector(self.onClick(sender:)), for: .touchUpInside) 42 btnWrite.backgroundColor = UIColor.blue 43 self.view.addSubview(btnWrite) 44 } 45 46 func onClick(sender:UIButton){ 47 if sender.tag == ButtonCamera { 48 openPicker(sourceType: UIImagePickerControllerSourceType.camera) 49 }else if sender.tag == ButtomRead { 50 openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary) 51 } 52 53 } 54 55 func showAlert(title: String?, text: String?) { 56 let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert) 57 alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 58 present(alert, animated: true, completion: nil) 59 } 60 61 func openPicker(sourceType:UIImagePickerControllerSourceType){ 62 if !UIImagePickerController.isSourceTypeAvailable(sourceType){ 63 showAlert(title: nil, text: "UnableUse") 64 return 65 } 66 67 let picker = UIImagePickerController() 68 picker.sourceType = sourceType 69 picker.delegate = self 70 71 present(picker, animated: true, completion: nil) 72 } 73 74 75 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 76 let image = info[UIImagePickerControllerOriginalImage]as! UIImage 77 imageView.image = image 78 79 picker.presentingViewController?.dismiss(animated: true,completion:nil) 80 } 81 82 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 83 picker.presentingViewController?.dismiss(animated: true, completion: nil) 84 } 85}

plistに以下を追加することも忘れずに。

xml

1<key>NSCameraUsageDescription</key> 2<string>カメラの使用許可をお願いします</string> 3<key>NSPhotoLibraryUsageDescription</key> 4<string>フォトライブラリの使用許可をお願いします</string> 5<key>UILaunchStoryboardName</key>

投稿2017/02/26 09:53

_Kentarou

総合スコア8490

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

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

退会済みユーザー

退会済みユーザー

2017/02/26 12:15

<plist version="1.0"> <dict> <key>NSCameraUsageDescription</key> <string>カメラの使用許可をお願いします</string> <key>NSPhotoLibraryUsageDescription</key> <string>フォトライブラリの使用許可をお願いします</string> <key>UILaunchStoryboardName</key> <key>CFBundleDevelopmentRegion</key> <string>en</string> <key>CFBundleExecutable</key> <string>$(EXECUTABLE_NAME)</string> <key>CFBundleIdentifier</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>$(PRODUCT_NAME)</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>1.0</string> <key>CFBundleVersion</key> <string>1</string> <key>LSRequiresIPhoneOS</key> <true/> <key>UILaunchStoryboardName</key> <string>LaunchScreen</string> <key>UIMainStoryboardFile</key> <string>Main</string> <key>UIRequiredDeviceCapabilities</key> <array> <string>armv7</string> </array> <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UISupportedInterfaceOrientations~ipad</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> </dict> </plist> のようにplistに記載したのですが、formatが違い読み込めない、と言われました。どのようにplistに記載すればいいのでしょうか?xcodeのInfo.plistからはどのように追加すればいいのかわかりませんでした..。
_Kentarou

2017/02/26 13:04

挿入するところが分からないのでしたら、無理してソースで入れるのでは無くて通常の`Property List`表示でkeyと文字列を設定してみてください。
退会済みユーザー

退会済みユーザー

2017/02/27 11:47

ありがとうございます!全部やってみたのですが、やはり動かず...。
退会済みユーザー

退会済みユーザー

2017/02/27 11:48

<dict> <key>UILaunchStoryboardName</key> <string></string> <key>CFBundleGetInfoString</key> <string></string> <key>NSPhotoLibraryUsageDescription</key> <string>フォトライブラリの使用許可をお願いします</string> <key>CFBundleDisplayName</key> <string></string> <key>NSCameraUsageDescription</key> <string>カメラの使用許可をお願いします</string> <key>LSApplicationCategoryType</key> <string></string> <key>CFBundleDevelopmentRegion</key> <string>en</string> <key>CFBundleExecutable</key> <string>$(EXECUTABLE_NAME)</string> <key>CFBundleIdentifier</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>$(PRODUCT_NAME)</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>1.0</string> <key>CFBundleVersion</key> <string>1</string> <key>LSRequiresIPhoneOS</key> <true/> <key>UIMainStoryboardFile</key> <string>Main</string> <key>UIRequiredDeviceCapabilities</key> <array> <string>armv7</string> </array> <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UISupportedInterfaceOrientations~ipad</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> </dict>
退会済みユーザー

退会済みユーザー

2017/02/27 11:49

のようにplistを書いたのですが、やはり何か間違ってますかね...?
退会済みユーザー

退会済みユーザー

2017/02/27 11:50

ちなみに、コントローラには import UIKit class SendController:UIViewController, UINavigationControllerDelegate,UIImagePickerControllerDelegate{ //定数 let ButtonCamera = 0 let ButtomRead = 1 let ButtonWrite = 2 //変数 var imageView:UIImageView = UIImageView() var btnCamera:UIButton = UIButton(type: .custom) var btnRead:UIButton = UIButton(type: .custom) var btnWrite:UIButton = UIButton(type: .custom) //ロード完了時に呼ばれる override func viewDidLoad() { super.viewDidLoad() // //カメラボタン生成 // self.view.addSubview(btnCamera) // //読み込み // self.view.addSubview(btnRead) // // イメージビューの生成 // self.view.addSubview(imageView) imageView.frame = CGRect(x: 150, y: 100, width: 200, height: 200) imageView.contentMode = .scaleAspectFit view.addSubview(imageView) btnCamera.frame = CGRect(x: 0, y: 100, width: 100, height: 100) btnCamera.setTitle("Camera", for: .normal) btnCamera.tag = ButtonCamera btnCamera.addTarget(self, action: #selector(self.onClick(sender:)), for: .touchUpInside) btnCamera.backgroundColor = UIColor.green self.view.addSubview(btnCamera) btnRead.frame = CGRect(x: 0, y: 200, width: 100, height: 100) btnRead.setTitle("Read", for: .normal) btnRead.tag = ButtomRead btnRead.addTarget(self, action: #selector(self.onClick(sender:)), for: .touchUpInside) btnRead.backgroundColor = UIColor.red self.view.addSubview(btnRead) btnWrite.frame = CGRect(x: 0, y: 300, width: 100, height: 100) btnWrite.setTitle("Write", for: .normal) btnWrite.tag = ButtonWrite btnWrite.addTarget(self, action: #selector(self.onClick(sender:)), for: .touchUpInside) btnWrite.backgroundColor = UIColor.blue self.view.addSubview(btnWrite) } //ボタンクリック時に呼ばれる func onClick(sender:UIButton){ if sender.tag == ButtonCamera { openPicker(sourceType: UIImagePickerControllerSourceType.camera) }else if sender.tag == ButtomRead { openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary) } } //アラートの表示 func showAlert(title: String?, text: String?) { let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) present(alert, animated: true, completion: nil) } //イメージピッカーのオープン func openPicker(sourceType:UIImagePickerControllerSourceType){ if !UIImagePickerController.isSourceTypeAvailable(sourceType){ showAlert(title: nil, text: "利用できません") return } //イメージピッカーの生成 let picker = UIImagePickerController() picker.sourceType = sourceType picker.delegate = self //ビューコントローラーのビューを開く present(picker, animated: true, completion: nil) } // // イメージピッカーのイメージ取得時に呼ばれる func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let image = info[UIImagePickerControllerOriginalImage]as! UIImage imageView.image = image //ビューコントローラーのビューを閉じる picker.presentingViewController?.dismiss(animated: true,completion:nil) } // //イメージピッカーのキャンセル取得時に呼ばれる func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.presentingViewController?.dismiss(animated: true, completion: nil) } }
退会済みユーザー

退会済みユーザー

2017/02/27 11:50

のように書きました
hassy_ta

2018/01/21 05:21

https://qiita.com/peka2/items/9a3f1b92cb4f88064b89 こちらもご覧になってください iOS11以降では<key>NSPhotoLibraryAddUsageDescription</key>も追記が必要です plistではどこに追記しても問題はないかと思います(もちろんstringとセットですが) それでならないのであれば具体的にどういうエラー文が出ているか、コピペ等でこちらに投げてみてはいかがでしょうか。より的確な対応ができるかなと。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問