问题
In obj-c with using image picker, I could get path of image with this:
NSURL* localUrl = (NSURL *)[info valueForKey:UIImagePickerControllerReferenceURL];
but when I try this code in swift :
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
var path : NSURL = editingInfo.valueForKeyPath("UIImagePickerControllerOriginalImage") as NSURL
... }
it crashes and say
fatal error: unexpectedly found nil while unwrapping an Optional value
so, how can I get path of an image in swift?
回答1:
you lost this line?
imagePicker.allowsEditing = true
if you set
imagePicker.allowsEditing = false
editingInfo will always be nil
回答2:
hi i got the image url using below code .It works fine in swift 2.0
let path = editingInfo[UIImagePickerControllerReferenceURL] as! NSURL
回答3:
You are perhaps using a different dictionary? Above it is info
, later you are using editingInfo
. Explain how you extract your dictionaries with complete code.
来源:https://stackoverflow.com/questions/26518304/how-to-get-path-of-image-selected-from-gallery-in-swift