didFinishPickingMediaWithInfo 未调用

didFinishPickingMediaWithInfo not called

我有一个 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)}

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

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

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

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

之外

Swift3中,现在调用这个委托方法:

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

区别是:

  1. 选择器前有下划线_
  2. 末尾的信息类型从 [String : AnyObject] 更改为 [String : Any]

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

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)
}

希望对您有所帮助:)

对 Swift 4.2 使用以下方法:

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

对于SWIFT 4.2

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

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

func openGallery()
    {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self

        imagePicker.sourceType = .photoLibrary

        self.present(imagePicker, animated: true, completion: nil)
   }

对于swift 4.2

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

对于Swift 4.2

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

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

}

随着 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)
    }
}

如果您的 UIImagePickerController() 对象将 allowsEditing 属性 设置为 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)

        }

可能有时候是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)
}

Swift5中我们需要使用:

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