-1

I have some code that will select an image from either the user's photo library or from their camera and display it in an image view. I'm accessing the photo URL from the following line of code:

let pickedPhotoURL: URL = (info["UIImagePickerControllerImageURL"] as? URL)!

This works perfectly when accessing a photo from the user's library. However, I'll always get a nil value at this line why trying to access the photo via the camera. Anyone have any ideas / suggestions?

narner
  • 2,729
  • 3
  • 22
  • 54

1 Answers1

1

It's because that value isn't set for a camera. Use UIImagePickerControllerOriginalImage (or edited image if you are using that).

That key gives you a UIImage -- if you need a URL, you need to save it somewhere.

Also, don't use strings, there are keys defined for you.

Lou Franco
  • 83,503
  • 14
  • 127
  • 183
  • Gothcya, thank you - so I'd have to save the photo and then extract the URL, correct? – narner Nov 10 '17 at 16:54
  • Yes. The URL will be a file:// to the location you saved it. – Lou Franco Nov 10 '17 at 18:46
  • Ahhh Ok, I see - I think that's what I was confused about (my original post wasn't that good). How would I access the url of the last image saved from a camera? – narner Nov 10 '17 at 19:22
  • You get an UIImage and then you save it. The url is "file://" + theFullPathOfTheFileYouJustSaved. See: https://stackoverflow.com/a/44354779/3937 – Lou Franco Nov 11 '17 at 14:11