【问题标题】:didFinishPickingMediaWithInfo is not calling in Xcode 12didFinishPickingMediaWithInfo 没有在 Xcode 12 中调用
【发布时间】:2020-09-29 14:38:12
【问题描述】:

我已经在 UIImagePickerController 上工作过。这段代码在 Xcode 11.3 中已经可以正常工作了。但是当我在 Xcode 12 上运行时,图像选择器 delegate 没有调用 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 

在图像选择器委托工作正常之后。但是当我删除断点时,它的委托没有调用。

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

【问题讨论】:

    标签: ios uiimagepickercontroller swift5 xcode12


    【解决方案1】:

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

    如果不是,也许你可以在前面加上pickerController.delegate = self,然后再试一次。

    【讨论】:

      【解决方案2】:

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

      例如我有这样的事情:

      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
      

      【讨论】:

      • 也许可以编辑您的问题并发布整个函数和任何相关代码,而不是几行单独的代码。它应该有帮助。
      【解决方案3】:

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

        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)
      

      【讨论】:

        【解决方案4】:

        我认为你犯了一个愚蠢的错误。只需更改几行代码即可。

        跟着我的脚步走

        => 这里 ProfileViewController 是我的 UIViewController,我将从 Gallery 中选择 Image 并将图像设置为 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多