【发布时间】:2022-01-22 10:24:45
【问题描述】:
我有两个按钮:一个用于拍照,一个用于从照片库中选择。他们都调用 UIImagePickerDelegate。问题是 UIImagePickerDelegate 在从库中选择照片后会被调用,但在拍照后不会被调用。因此,当我拍照并按“使用”时,我会回到我的视野,但什么也没有发生。这是我的代码:
open class BackgroundImageViewController: UIViewController, BackgroundViewController, UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
@IBAction func didTapTakePhoto(_ sender: UIButton) {
imagePicker.sourceType = .camera
imagePicker.allowsEditing = true
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
// This part works fine, view for taking picture is presented, and I can take a picture successfully.
}
@IBAction func didTapChoosePhoto(_ sender: UIButton) {
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}
}
extension BackgroundImageViewController: UIImagePickerControllerDelegate {
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// This method is getting called only when I choose the photo from the library, not when I take the picture.
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
imageView.image = image
picker.dismiss(animated: true, completion: nil)
}
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
这个问题让我发疯,因为两个按钮的代码几乎相同。当然,我在info.plist 文件中设置了所有必要的权限:
感谢任何帮助!
【问题讨论】:
-
也许你不小心在你的应用程序中拒绝了访问相机的权限。在 XCode 中构建和运行应用程序之前,请尝试从设备中删除您的应用程序。
-
我已经完成了,它没有帮助。另外我已经提到我可以拍照,但我不能使用它,因为不会调用委托。
-
同样的代码对我有用。