Swift UIImagePickerController动态转换在获取路径时失败

时间:2015-02-18 23:01:50

标签: ios swift path uiimagepickercontroller

我试图从UIImagePicker获取照片的路径,并且我得到了#34; Swift动态演员失败"当我试图得到它。这是我的代码:

@IBAction func addPhotoButton(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){

        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
        imagePicker.allowsEditing = true

        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
}

在这里我得到了路径:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
    self.dismissViewControllerAnimated(true, completion: { () -> Void in
        var path: NSURL = editingInfo.valueForKeyPath("UIImagePickerControllerOriginalImage") as NSURL
        println(path)
    })
}

当我尝试获取var path

的值时,应用程序崩溃了

1 个答案:

答案 0 :(得分:2)

UIImagePickerControllerOriginalImage键将返回UIImage而不是所选图片的NSURL

如果您需要引用网址,则需要使用UIImagePickerControllerReferenceURL密钥。

更改以下代码:

var path: NSURL = editingInfo.valueForKeyPath("UIImagePickerControllerOriginalImage") as NSURL

为:

var path: NSURL = editingInfo.valueForKey("UIImagePickerControllerReferenceURL") as NSURL

  

UIImagePickerControllerOriginalImage

     

指定用户选择的原始未剪切图像。

     

此键的值为UIImage对象。


  

UIImagePickerControllerReferenceURL

     

所选项目原始版本的资产库网址。

     

用户编辑拾取的项目后 - 例如通过裁剪图像或   修剪电影 - URL继续指向原始版本   挑选的项目。

     

此键的值是NSURL对象。

参考UIImagePickerControllerDelegate Protocol Reference