Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didFinishPickingMediaWithInfo not called

I have a UITableViewCell button to take an image and place it back in the cell. When I call the UIImagePickerController and take the image, it doesn't call the following delegate:

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

This is my takePhotoFunction in UITableViewController:

@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)}
like image 859
Tal Zion Avatar asked Mar 22 '15 00:03

Tal Zion


4 Answers

In Swift 3, this delegate method is now called:

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

The differences are:

  1. There is an underline _ before picker
  2. info type at the end is changed from [String : AnyObject] to [String : Any]

I had the same issue and when I made these changes it worked.

like image 139
JPetric Avatar answered Nov 07 '22 04:11

JPetric


1. Dont forget to add UIImagePickerControllerDelegate,UINavigationControllerDelegate
2. In your viewDidLoad() add picker.delegate = self
3. Add the delegate methods

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

Hope it helps :)

like image 23
Vinu David Jose Avatar answered Nov 07 '22 04:11

Vinu David Jose


For swift 4.2

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
   if let chosenImage = info[.originalImage] as? UIImage{
        //use image
    }
}
like image 7
Atul Pol Avatar answered Nov 07 '22 06:11

Atul Pol


With the Swift 5 we have slight update.

Here is single screen example I have created to test this with 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)
    }
}
like image 3
swiftBoy Avatar answered Nov 07 '22 06:11

swiftBoy