【发布时间】:2016-08-09 18:42:57
【问题描述】:
我正在玩 ios swift 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)
}
【问题讨论】: