Reza_Rg
Reza_Rg

Reputation: 3374

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

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?

Upvotes: 1

Views: 4388

Answers (3)

Jestin Francis
Jestin Francis

Reputation: 323

hi i got the image url using below code .It works fine in swift 2.0

let path = editingInfo[UIImagePickerControllerReferenceURL] as! NSURL

Upvotes: 2

Steven Wang
Steven Wang

Reputation: 21

you lost this line?

imagePicker.allowsEditing = true

if you set

imagePicker.allowsEditing = false

editingInfo will always be nil

Upvotes: 2

Mundi
Mundi

Reputation: 80265

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.

Upvotes: 0

Related Questions