选择照片时imagePickerController错误

时间:2015-08-19 19:29:36

标签: ios iphone xcode swift uiimagepickercontroller

我正在使用imagePickerController,但是当我选择imagen时出现错误:

致命错误:在解包可选值时意外发现nil

我不知道为什么因为我用展开价值如果让

  func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

    if let img = info[UIImagePickerControllerOriginalImage] as? UIImage {
        photoImageView.image = img
    }

    dismissViewControllerAnimated(true, completion: nil)
}

2 个答案:

答案 0 :(得分:2)

错误发生在

photoImageView.image = img

在这种情况下,可能是photoImageView是nil(它通常是一个隐式解包的可选项)。

从评论中更新:

请检查photoImageView是否为@IBOutlet,并且它已连接到故事板上的UIImageView。如果以编程方式创建它,请尝试在设置图像之前将其添加到窗口中的视图。

答案 1 :(得分:0)

VC调用可能有问题。这段代码工作正常:

let picker = UIImagePickerController()
            picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            picker.allowsEditing  = false
            picker.delegate = self
            self.presentViewController(picker, animated: true, completion: nil)



func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    productImageView.image = image
    dismissViewControllerAnimated(true, completion: nil)
}