How to get path of image, selected from gallery in swift

对着背影说爱祢 提交于 2019-12-11 07:44:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!