Search code examples
swiftxcodeuiimageviewios12

I'm trying to display an image from the photo library in a UIImageView


The full error is:

2018-11-17 11:48:21.587818-0700 TestApp[3763:162426] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

My code:

import UIKit

class CameraViewController: UIViewController {

@IBOutlet weak var captionTextView: UITextView!
@IBOutlet weak var photo: UIImageView!
@IBOutlet weak var shareButton: UIButton!
var selectedImage: UIImage?
override func viewDidLoad() {
    super.viewDidLoad()

    let tapGesturn = UITapGestureRecognizer(target: self, action: #selector(self.handleSelectPhoto))
    photo.addGestureRecognizer(tapGesturn)
    photo.isUserInteractionEnabled = true

}

@objc func handleSelectPhoto() {
    let pickerController = UIImagePickerController()
    pickerController.delegate = self
    present(pickerController, animated: true, completion: nil)
}

@IBAction func shareButton_TouchUpInside(_ sender: Any) {

}
}
 extension CameraViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("did Finish Picking Media")
    if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage{
        selectedImage = image
        photo.image = image
    }
    dismiss(animated: true, completion: nil)
  }
}

Solution

  • Instead of using string "UIImagePickerControllerOriginalImage" as key for info dictionary use info key for original image UIImagePickerController.InfoKey.originalImage

    So replace this

    if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
    

    with this

    if let image = info[.originalImage] as? UIImage {
    

    also replace info dictionary type in parameters of delegate method from [String : Any] to [UIImagePickerController.InfoKey : Any]

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

    and last replace private keyword before delegate method with @objc