Ios didFinishPickingMediaWithInfo未在Xcode 12中调用

Ios didFinishPickingMediaWithInfo未在Xcode 12中调用,ios,uiimagepickercontroller,swift5,xcode12,Ios,Uiimagepickercontroller,Swift5,Xcode12,我已经在UIImagePickerController上工作过了。这段代码在Xcode 11.3中已经可以正常工作了。但是,当我在Xcode 12上运行时,图像选择器代理并没有调用Xcode 12 /// Picked Image struct PickedImage { var image: UIImage? var api: String? } /// Image picker class ImagePicker: NSObject { typea

我已经在UIImagePickerController上工作过了。这段代码在Xcode 11.3中已经可以正常工作了。但是,当我在Xcode 12上运行时,图像选择器代理并没有调用Xcode 12

/// Picked Image
struct PickedImage {
    var image: UIImage?
    var api: String?
}

/// Image picker
class ImagePicker: NSObject {
        
    typealias ImagePickerHandler = ((_ selected: PickedImage) -> ())
    
    private weak var presentationController: UIViewController?
    
    let pickerController: UIImagePickerController = UIImagePickerController()

    var apiKey: String?
    
    private var handler: ImagePickerHandler? = nil
    
    private func action(for type: UIImagePickerController.SourceType, title: String) -> UIAlertAction? {
        
        guard UIImagePickerController.isSourceTypeAvailable(type) else {
            return nil
        }
        
        return UIAlertAction(title: title, style: .default) { (action) in
            
            DispatchQueue.main.async {
                
                self.pickerController.mediaTypes = ["public.image"]
                self.pickerController.sourceType = type
                self.pickerController.delegate = self
                
                self.presentationController?.present(self.pickerController, animated: true, completion: {
                })
                
            }
        }
    }
    
    /// Present source view
    /// - Parameter sourceView: view
    func present(presentationController: UIViewController, completed: ImagePickerHandler? = nil) {
        
        self.handler = completed
        
        self.presentationController = presentationController
        // self.delegate = delegate
        
        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        
        if let action = self.action(for: .camera, title: "Take photo") {
            alertController.addAction(action)
        }
        if let action = self.action(for: .savedPhotosAlbum, title: "Camera roll") {
            alertController.addAction(action)
        }
        if let action = self.action(for: .photoLibrary, title: "Photo library") {
            alertController.addAction(action)
        }
        
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        
        //            if UIDevice.current.userInterfaceIdiom == .pad {
        //                alertController.popoverPresentationController?.sourceView = sourceView
        //                alertController.popoverPresentationController?.sourceRect = sourceView.bounds
        //                alertController.popoverPresentationController?.permittedArrowDirections = [.down, .up]
        //            }
        self.presentationController?.present(alertController, animated: true)
        
    }
    
    private func pickerController(didSelect image: UIImage?, imageURL: URL?) {
        
        pickerController.dismiss(animated: true, completion: nil)
        // self.delegate?.imagePicker(picker: self, didSelected: image, apikey: apiKey)
        
        handler?(PickedImage(image: image, api: apiKey))
    }
}

/// ImagePicker controller delegate
extension ImagePicker: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        
        self.pickerController(didSelect: nil, imageURL: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        self.pickerController(didSelect: info[.originalImage] as? UIImage, imageURL: info[.imageURL] as? URL)
    }
}
当我检查委托是否已应用或未使用断点时。就像控制台里的意思一样

po imagepicker.delegate 
之后,图像选择器代理工作正常。但当我删除断点时,它的委托并没有调用


我不知道原因是什么。为什么它不起作用。我可以知道如何解决此问题。

是否有任何理由不将
pickerController.delegate=self
放在
self.presentationController?的前面。present(pickerController,动画:true,完成:{})


如果没有,您可以在前面添加
pickerController.delegate=self
,然后重试。

这很可能是因为您没有将picker控制器保留在变量中。一旦函数完成,它就会被释放

例如,我有这样的东西:

class MyClass: UIImagePickerControllerDelegate {
   let imagePicker = UIImagePickerController()
}

func pickImageFromGallery() {
    self.imagePicker.delegate = self
    self.imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
    self.imagePicker.allowsEditing = false
    self.present(self.imagePicker, animated: true, completion: nil)
}

... and the delegate methods as well

我认为你犯了一个愚蠢的错误。只需更改几行代码,就可以开始了

跟着我的脚步走

=>这里ProfileViewController是我的UIViewController,我将从Gallery中选择图像并将图像设置为UIImageView。您必须在要拾取图像的位置使用UIViewController

ProfileViewController: UIViewController{

let pickerController = UIImagePickerController()

viewDidLoad(){

}

@IBAction func pickImageAction(sender: UIButton){
 self.openImagePicker()
}

func openImagePicker()
{
pickerController.delegate = self
pickerController.allowsEditing = true
pickerController.mediaTypes = ["public.image", "public.movie"]
pickerController.sourceType = .photoLibrary // Pick image from PhotoLibrary  
//pickerController.sourceType = .savedPhotosAlbum // Pick Saved Images
//pickerController.sourceType = .camera  // Click Image using Camera
self.present(pickerController, animated: true)
} //End of setupImagePicker


} // End of ProfileViewController

你的代码总是错的;如果它能起作用,那就太幸运了:

  let pickerController: UIImagePickerController = UIImagePickerController()
  pickerController.mediaTypes = ["public.image"]
  pickerController.sourceType = "Photo library"
  self.presentationController?.present(pickerController, animated: true, completion: {
      pickerController.delegate = self
  })
改为:

  let pickerController: UIImagePickerController = UIImagePickerController()
  pickerController.mediaTypes = ["public.image"]
  pickerController.sourceType = .photoLibrary
  pickerController.delegate = self
  self.present(pickerController, animated: true)

也许可以编辑您的问题并发布整个函数和任何相关代码,而不是几个单独的行。这应该会有帮助。
  let pickerController: UIImagePickerController = UIImagePickerController()
  pickerController.mediaTypes = ["public.image"]
  pickerController.sourceType = .photoLibrary
  pickerController.delegate = self
  self.present(pickerController, animated: true)