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

时间:2018-11-17 18:52:21

标签: swift xcode uiimageview ios12

完整错误是:

  

2018-11-17 11:48:21.587818-0700 TestApp [3763:162426] [发现]   发现扩展名时遇到的错误:错误   域= PlugInKit代码= 13“查询已取消”   UserInfo = {NSLocalizedDescription =查询已取消}

我的代码:

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

1 个答案:

答案 0 :(得分:2)

不是使用字符串"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