Melanie Journe
Melanie Journe

Reputation: 1379

Swift - ImagePicker not executing delegate

I just want to display an image taken with the iPhone camera in a ImageView. That was the first step of the project but it doesn't work...

Here is my code :

import UIKit
import MobileCoreServices

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var scanButton: UIButton!
    @IBOutlet weak var textfield: UITextField!

     let imagePicker = UIImagePickerController()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.imagePicker.modalPresentationStyle = UIModalPresentationStyle.currentContext
        self.imagePicker.delegate = self
        self.imagePicker.mediaTypes = [kUTTypeImage as String]
        self.imagePicker.sourceType = .camera
        self.imagePicker.allowsEditing = true;


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK: - Actions

    @IBAction func scanAction(_ sender: UIButton) {

        print("----- scanAction -----");

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


    //MARK: - UIImagePickerControllerDelegate

    private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        print("----- didFinishPickingMediaWithInfo -----");

        var image:UIImage!;
        if let  pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
            image = pickedImage;
        }
        else {
            image = info[UIImagePickerControllerOriginalImage] as! UIImage;
        }

        self.imageView.image = image;


        dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        print("----- imagePickerControllerDidCancel -----");

        dismiss(animated: true, completion: nil)
    }

}

I see the print of the action button but I never see the following one :

print("----- didFinishPickingMediaWithInfo -----");

I already have done imagePicker like this in the past so I don't understand why it doesn't work.

Can someone help me please?

Upvotes: 2

Views: 219

Answers (1)

Nirav D
Nirav D

Reputation: 72410

The type of info is changed to [String : Any] in Swift 3 instead of [String : AnyObject] also you have not added _ as first parameter label, so change your method like this.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("----- imagePickerControllerDidCancel -----");
}

Upvotes: 3

Related Questions