私はios swift foodtrackerの例で遊んでいる。 デフォルトの最終的な例では、ユーザーがライブラリから写真を選択できるようにしています。 しかし、カメラを使って写真を撮ってもらいたいです。 以下のコードを試しましたが、機能は変更されませんでした。ユーザーのオプション設定(カメラ/フォトライブラリ)を尋ねることなく、まだ写真ライブラリに行きました。私はユーザーがカメラを使って写真を撮るか、図書館(ios-swift、apple example- foodtracker)から写真を選ぶかを選択できるようにしたい。
私は次のエラーメッセージ取得しています:それは割り当て解除されている間、ビューコントローラのビューをロードしようとすると
は(許可されていないと、未定義の動作を引き起こす場合があります)
そして、これを私が使用したコードは次のとおりです。
@IBAction func selectImageFromPhotoLibrary(sender: AnyObject) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: {
action in
imagePickerController.sourceType = .Camera
self.presentViewController(imagePickerController, animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Photo Library", style: .Default, handler: {
action in
imagePickerController.sourceType = .PhotoLibrary
self.presentViewController(imagePickerController, animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(imagePickerController, animated: true, completion: nil)
}