didFinishPickingMediaWithInfo不叫Swift

Tal*_*ion 21 iphone uitableview uiimagepickercontroller ios swift

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

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject], sender:AnyObject)
Run Code Online (Sandbox Code Playgroud)

这是我在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)}
Run Code Online (Sandbox Code Playgroud)

JPe*_*ric 40

Swift 3中,现在调用此委托方法:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
Run Code Online (Sandbox Code Playgroud)

不同之处是:

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

我有同样的问题,当我做出这些改变时,它起作用了.


Vin*_*ose 15

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)
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你 :)


小智 5

快速4.2

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