我试图从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
答案 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
对象。