【问题标题】:UIImagePicker delegate doesn't get calledUIImagePicker 委托没有被调用
【发布时间】: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 中构建和运行应用程序之前,请尝试从设备中删除您的应用程序。
  • 我已经完成了,它没有帮助。另外我已经提到我可以拍照,但我不能使用它,因为不会调用委托。
  • 同样的代码对我有用。

标签: ios swift delegates


【解决方案1】:

从 ios 14 开始建议使用 PHPickerViewController

@available(iOS 14, *)
extension YourViewController : PHPickerViewControllerDelegate {
    public func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        picker.dismiss(animated: true, completion: nil)
        guard !results.isEmpty else { return }
        
        if imageResult.itemProvider.canLoadObject(ofClass: UIImage.self) {
            imageResult.itemProvider.loadObject(ofClass: UIImage.self) { (selectedImage, error) in
                //Selected Image is what you need
            }}
    }
    
    func openGaleryPHPicker(){
        
        //let library = PHPhotoLibrary.shared()
        var config = PHPickerConfiguration(photoLibrary: .shared())
        config.selectionLimit = 1
        config.filter = PHPickerFilter.images
        config.preferredAssetRepresentationMode = .automatic
        
        let pickerViewController = PHPickerViewController(configuration: config)
        pickerViewController.delegate = self
        
        present(pickerViewController, animated: true)
    }
}

对于请求授权也应该写成

if #available(iOS 14, *) {

PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
    switch status {
    case .notDetermined:
        // The user hasn't determined this app's access.
        PHPhotoLibrary.requestAuthorization(for: .readWrite) {
            status in
            switch status  {
            case .authorized:
                DispatchQueue.main.async {
                    self.openGaleryPHPicker(isLimited: false)
                }
            case .limited:
                DispatchQueue.main.async {
                    self.openGaleryPHPicker(isLimited: true)
                }
            case .notDetermined:
                break
            case .denied, .restricted:
                self.requestToOpenSettings()
            }
        }
    case .restricted, .denied:
        self.requestToOpenSettings()
        // The system restricted this app's access.
        // The user explicitly denied this app's access.
    case .authorized:
        DispatchQueue.main.async {
            self.openGaleryPHPicker(isLimited: false)
        }
    case .limited:
        // The user authorized this app to access Photos data.
        DispatchQueue.main.async {
            self.openGaleryPHPicker(isLimited: true)
        }
        
        // The user authorized this app for limited Photos access.
    @unknown default:
        fatalError()
    }
}
}

【讨论】:

    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多