XCODE 9中的UIImagePickerController出错

时间:2017-11-22 15:18:08

标签: ios xcode

当我执行mi代码时显示错误:

发现扩展时遇到错误:错误Domain = PlugInKit Code = 13“查询已取消”UserInfo = {NSLocalizedDescription = query canceled}

当我从库中选择照片或从库中取消时会发生这种情况,问题是照片会出现但是没有分配给变量。

我的代码:

class DetalleShopViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var imageDetalle: UIImageView!
@IBOutlet weak var textFieldDetalle: UITextField!
@IBOutlet weak var saveBtn: UIBarButtonItem!

var shops: [NSManagedObject] = []

override func viewDidLoad() {
    super.viewDidLoad()
    textFieldDetalle.delegate = self
    // Do any additional setup after loading the view.
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (sender as AnyObject? !== saveBtn) {return}
    print("VOY A MANDARLA")
    self.save(name: textFieldDetalle.text!, image: imageDetalle.image!)
    print("MANDADA")
}


@IBAction func cancelar(_ sender: UIBarButtonItem) {
    dismiss(animated: true, completion: nil)
}

@IBAction func seleccionImagen(_ sender: UITapGestureRecognizer) {
    textFieldDetalle.resignFirstResponder()
    let imagePickerCtrl = UIImagePickerController()
    imagePickerCtrl.delegate = self
    imagePickerCtrl.sourceType = .photoLibrary
    imagePickerCtrl.allowsEditing = false

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


// MARK: UITextFieldDelegate

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    self.dismiss(animated: true, completion: nil)
}

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

    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    imageDetalle.image = selectedImage
    self.dismiss(animated: true, completion: nil)
}

2 个答案:

答案 0 :(得分:0)

也许试图解雇自己(又名当前的VC)正在取消选择器操作?

尝试

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

而不是

self.dismiss(animated: true, completion: nil)

答案 1 :(得分:0)

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
                imageDetalle.image = selectedImage
    }
self.dismiss(animated: true, completion: nil)

将ur picker控制器声明为类变量,并在viewdidload中设置其委托。