【问题标题】:didFinishPickingMediaWithInfo not called未调用 didFinishPickingMediaWithInfo
【发布时间】:2015-03-22 00:14:47
【问题描述】:

我有一个UITableViewCell 按钮来拍摄图像并将其放回单元格中。当我调用UIImagePickerController 并拍摄图像时,它不会调用以下委托:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject], sender:AnyObject)

这是我在UITableViewController 中的takePhotoFunction

@IBAction func takePhoto(sender: AnyObject) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true

let actionSheet = UIAlertController(title: "Choose image souruce", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

actionSheet.addAction(UIAlertAction(title: "Take Image", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
    imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
    self.presentViewController(imagePickerController, animated: true, completion: nil)
}))

actionSheet.addAction(UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
    imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    self.presentViewController(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)}

【问题讨论】:

    标签: ios uiimagepickercontroller


    【解决方案1】:

    Swift 3 中,现在调用了这个委托方法:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    

    区别在于:

    1. 在选择器之前有一个下划线_
    2. 最后的信息类型从 [String : AnyObject] 更改为 [String : Any]

    我遇到了同样的问题,当我进行这些更改时,它起作用了。

    【讨论】:

      【解决方案2】:

      1.不要忘记添加UIImagePickerControllerDelegate,UINavigationControllerDelegate
      2. 在您的viewDidLoad() 中添加picker.delegate = self
      3. 添加委托方法

      public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
              let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
              userImageView.contentMode = .scaleAspectFit
              userImageView.image = chosenImage
              dismiss(animated:true, completion: nil)
          }
      
      public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {self.dismiss(animated: true, completion: nil)
      }
      

      希望对你有帮助:)

      【讨论】:

        【解决方案3】:

        对于 Swift 4.2

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
           if let chosenImage = info[.originalImage] as? UIImage{
                //use image
            }
        }
        

        【讨论】:

        • UIImage?' is not convertible to 'UIImage'
        • Cast from 'Slice<Dictionary<UIImagePickerController.InfoKey, Any>>' (aka 'Slice<Dictionary<NSString, Any>>') to unrelated type 'UIImage' always fails
        【解决方案4】:

        随着 Swift 5 我们有轻微的更新。

        这是我创建的单屏示例,用于在 iOS 12.2 上进行测试

        import UIKit
        
        
        class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        
            @IBOutlet weak var imageView: UIImageView!
        
            override func viewDidLoad() {
                super.viewDidLoad()
            }
        
            //MARK: Open Photos Galley Button Action method
            @IBAction func openPhotosGallery(_ sender: Any) {
        
                if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
                    let myPickerController = UIImagePickerController()
                    myPickerController.delegate = self;
                    myPickerController.sourceType = .photoLibrary
                    self.present(myPickerController, animated: true, completion: nil)
                }
            }
        
            //MARK: ImagePicker Controller Delegate methods
            func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
                self.dismiss(animated: true, completion: nil)
            }
        
            func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
                let chosenImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
                imageView.image = chosenImage
                dismiss(animated:true, completion: nil)
            }
        }
        

        【讨论】:

          【解决方案5】:

          在 Swift 4.2 中使用以下方法:

          public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
          }
          

          【讨论】:

            【解决方案6】:

            对于 Swift 4.2

            此方法已重命名,现在应按如下方式调用。我遇到了这个委托方法没有调用的相同问题,但在此之后,我的问题解决了。

            func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            
            }
            

            【讨论】:

              【解决方案7】:

              对于SWIFT 4.2

              当程序未调用委托方法时,我遇到了类似的问题。

              imagePicker.delegate = self 应该是 NOT 在 vi​​ewDidLoad() 方法中,但在打开画廊的方法中。像这样:

              func openGallery()
                  {
                      let imagePicker = UIImagePickerController()
                      imagePicker.delegate = self
              
                      imagePicker.sourceType = .photoLibrary
              
                      self.present(imagePicker, animated: true, completion: nil)
                 }
              

              【讨论】:

                【解决方案8】:

                您必须以这种方式使用该方法,因为您没有声明任何委托方法:

                func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:[NSObject : AnyObject]) {
                        println("Calling")       
                }
                

                Apple 文档参考:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIImagePickerControllerDelegate/imagePickerController:didFinishPickingMediaWithInfo:

                【讨论】:

                • 您好 Dheeraj,感谢您的回答。我已经实现了委托方法,但是在 imagpepickercontroller 完成后,这个方法没有被调用。
                • 您是否使用了我提到的委托方法,因为您的委托方法不正确,并且没有您声明的委托方法。
                【解决方案9】:

                委托方法现在称为func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])

                我看到您的 imagePickerController.delegate 设置正确,所以我假设您不小心将 didFinishPickingMediaWithInfo 代码放在 ViewController 类之外

                【讨论】:

                  【解决方案10】:

                  如果你的 UIImagePickerController() 对象的 allowEditing 属性设置为 true,那么你应该在委托方法中获取图像:

                  如果allowsEditing == true

                  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
                  
                          if let pickedImage = info[.editedImage] as? UIImage {
                  
                                //do something with image
                  
                          }
                  
                             dismiss(animated: true, completion: nil)
                  
                      }
                  

                  如果allowsEditing == false

                  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
                  
                              if let pickedImage = info[.originalImage] as? UIImage {
                  
                                    //do something with image
                  
                              }
                  
                                 dismiss(animated: true, completion: nil)
                  
                          }
                  

                  【讨论】:

                    【解决方案11】:

                    也许有时它是由 ARC 引起的? 我在控制器中而不是在函数中声明了一个适配器变量(对于下面的 pre-iOS14 存在 UIImagePickerController 或对于 iOS 14+ 存在 PHPickerViewController)。 它对我有用。

                    var adapter: PHPickerAdapter?
                    
                    @objc func openGallery(recognizer: UITapGestureRecognizer) {
                        adapter = PHPickerAdapter()
                        adapter?.delegate = self
                        adapter?.showPhotoPicker(.photoLibrary, from: self)
                    }
                    

                    【讨论】:

                      【解决方案12】:

                      Swift 5 中我们需要使用:

                      func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])  {
                      

                      【讨论】:

                        猜你喜欢
                        • 2018-09-01
                        • 1970-01-01
                        • 2014-07-28
                        • 1970-01-01
                        • 1970-01-01
                        • 2021-01-27
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多