didFinishPickingMediaWithInfo未在Xcode 12中调用



我已经使用了UIImagePickerController。此代码在Xcode11.3中已经可以正常工作。但当我在Xcode12上运行图像选择器时,委托没有调用Xcode12

/// 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 

在图像选取器代理工作正常之后。但当我删除断点时,它的委托不会调用。

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

这很可能是因为您没有在变量中保留选择器控制器。一旦您的函数完成,它就会被解除分配。

例如,我有这样的东西:

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

有什么理由不把pickerController.delegate = self放在self.presentationController?.present(pickerController, animated: true, completion: {})'之前吗?

如果没有,也许您可以将pickerController.delegate = self放在它之前,然后重试。

您的代码总是错误的;如果它真的成功了,那就太幸运了:

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)

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

只需按照我的步骤

=>这里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
// MARK:- Delegate method for UIImagePicker
extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate  {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
// image is your image which you picked from Image Gallery or using Camera.
// you can set this image directly to your UIImageView.
// self.yourImageView.image = image
// or you can compress this image, converting to JPG image type.
// compressionQuality will reduce your image quality and Image Size.
if let jpegData = image.jpegData(compressionQuality: 0.8) {
// you can use this compressed image.
}
self.dismiss(animated: true)

}//  End of didFinishPickingMediaWithInfo 

} // End of Extension

相关内容

  • 没有找到相关文章

最新更新