ImagePicker和分配给ImageView的问题

时间:2019-03-16 23:28:12

标签: ios swift

我正在构建一个小型应用程序,该应用程序允许用户选择要添加到帖子的图像。用户选择添加的图像(具有点击手势识别器),并显示iOS照片库。然后,用户选择图像,并且所选图像应替换默认的相机图像。从库中选择图像时,出现以下错误:2019-03-16 19:13:09.238959-0400 Spirit-App[34772:2211555] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}。我有enter image description here

的info.plist设置

基于我用于调试的打印语句,我似乎正在进入imageTapped函数,但从未进入@objc func imagePickerController。即使我注释掉@objc func imagePickerController,我仍然收到上面的错误。因此,有两个问题:

  1. 即使我没有在模式页面上重新分配所选图像,为什么也收到错误消息?
  2. 如果我解决了该问题,我拥有的分配图像的代码是否可以正常工作或需要更改?

代码:

class NewPostViewController: ViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var newPostImage: UIImageView!
    @IBOutlet weak var postDescription: UITextView!

    let imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.clear
        view.isOpaque = false
        imagePicker.delegate = self
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tap:)))
        newPostImage.isUserInteractionEnabled = true
        newPostImage.addGestureRecognizer(tap)
    }

    @objc func imageTapped(tap: UITapGestureRecognizer) {
        print("BUTTON WORKED!")
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .photoLibrary

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

    @objc func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
        self.dismiss(animated: true, completion: { () -> Void in
        })

        print("Image Picker Complete")
        newPostImage.image = image
        dismiss(animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:1)

您对委托方法的签名错误。

更改:

@objc func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){

收件人:

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

您还需要更改该委托中的所有代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[.originalImage] as? UIImage {
        newPostImage.image = image
    }

    picker.dismiss(animated: true, completion: nil)
}