Swift:UIImagePickerController - 在解包Optional值时意外地发现了nil

时间:2014-10-23 20:07:30

标签: xcode swift

我正在尝试使用UIImagePickerController在我的应用中拍摄图片。视图加载正确,拍摄照片后的方法被正确调用。但是在我的方法中,当我试图使用拍摄的图像时,我得到以下错误。错误出现在我将 UIImage 设置为 UIImageView 的行中。

Unexpectedly found nil while unwrapping an Optional value

这是我的方法:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage pickerImage: UIImage!, editingInfo: NSDictionary!) {
    var insertViewControllerStoryboard = self.storyboard?.instantiateViewControllerWithIdentifier("InsertViewController");
    self.showViewController((insertViewControllerStoryboard as InsertViewController), sender: insertViewController);

    insertViewController.imageView.image = pickerImage;

    self.dismissViewControllerAnimated(true, completion: nil);
}

当我记录变量 pickerImage 时,变量不是nil。变量用UIImage填充。为什么会出现此错误?

1 个答案:

答案 0 :(得分:0)

试试这个:

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

    ....

    var pickerImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
    insertViewController.imageView.image = pickerImage;