未调用 UIImagePickerControllerDelegate

UIImagePickerControllerDelegate not called

我有一个独立的 class 可以使用 UIImagePickerController 从 camera/photo 库中获取图像。

我似乎无法弄清楚为什么没有调用 didFinishPickingMediaWithInfo 或 imagePickerControllerDidCancel 方法..?我意识到这个问题已被问过很多次,但我无法弄清楚...我知道所有方法名称都是正确的。

我已在 info.plist 中正确设置以下所有内容:


import UIKit

class ImagePickerService: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

  fileprivate var completion: ((UIImage) -> ())
  fileprivate var imagePicker: UIImagePickerController

  init(completion: @escaping ((UIImage) -> ())) {
    self.completion = completion
    self.imagePicker = UIImagePickerController()
    super.init()
    imagePicker.delegate = self
  }

  func show(from viewController: UIViewController) {
    showAlert(from: viewController)
  }

  fileprivate func showAlert(from viewController: UIViewController) {
    let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Take a photo", style: .default) { _ in
      self.openCamera(from: viewController)
    })
    alert.addAction(UIAlertAction(title: "Choose from library", style: .default) { _ in
      self.openGallery(from: viewController)
    })
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    viewController.present(alert, animated: true, completion: nil)
  }

  // MARK: Image Picker

  fileprivate func openCamera(from viewController: UIViewController) {
    guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
      viewController.showOKAlert(title: "Error!", message: "No camera available")
      return
    }
    showImagePicker(withSource: .camera, from: viewController)
  }

  fileprivate func openGallery(from viewController: UIViewController) {
    showImagePicker(withSource: .photoLibrary, from: viewController)
  }

  fileprivate func showImagePicker(withSource source: UIImagePickerControllerSourceType, from viewController: UIViewController) {
    imagePicker.sourceType = source
    viewController.present(imagePicker, animated: true, completion: nil)
  }

}

extension ImagePickerService {

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else {
      return
    }
    completion(image)
    picker.dismiss(animated: true, completion: nil)
  }

  func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
  }

}

编辑: 问题是我没有在实例化时创建对 ImagePickerService 的强引用,它在调用委托方法之前自动释放。

问题出在以下代码片段中:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else {
      return
    }
    completion(image)
    picker.dismiss(animated: true, completion: nil)
}

您希望始终有经过编辑的图像,但您的照片库中的所有照片可能并非如此。当 info[UIImagePickerControllerEditedImage] returns nil 时尝试使用 UIImagePickerControllerOriginalImage 键。

如果一开始没有调用这些方法,您需要确保对 ImagePickerService 实例有强引用。

例如,这段代码有效:

class ViewController: UIViewController {

    var picker: ImagePickerService?

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { [weak self] in
            guard let `self` = self else { return }
            self.picker = ImagePickerService() { (image) in
                print(image)
            }
            self.picker?.show(from: self)
        }
    }

}

尝试创建 ImagePickerService 的单例Class:

static let shared = ImagePickerService()

创建单例后,对服务对象的引用将保持不变,不会被释放。

在 init() 中设置 delegate 并制作一个显示 ImagePicker 的方法。

func pickMedia(from: UIViewController?,didPickMedia: ((UIImage) -> ())?){      
            //self.openCamera()
            //or
            //self.openGallery()
}

从那个单例中,您可以将其呈现为:

  ImagePickerService.shared.pickMedia(from: self) { (image) in

  }