我正在尝试在 UIImageView 中显示照片库中的图像

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

完整的错误是:

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}

我的代码:

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

不要使用字符串 "UIImagePickerControllerOriginalImage" 作为信息字典的键,而是使用原始图像的信息键 UIImagePickerController.InfoKey.originalImage

所以替换这个

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

有了这个

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

也将委托方法参数中的信息字典类型从[String : Any]替换为[UIImagePickerController.InfoKey : Any]

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

最后将委托方法前的 private 关键字替换为 @objc