3

I'm trying to print all the image metadata in the imagePickerController: didFinishPickingMediaWithInfo function. When I use the info.objectForKey(UIImagePickerControllerReferenceURL) method, it returns nil and if try to use this result my app crashes. Does anyone know why it returns nil and what else can I use to print all the image metadata when I pick an image? (using UIImageJPEGRepresentation is not an option because the EXIF data is removed). This is my code:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:     NSDictionary!)
{
let image = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage

let refURL : NSURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL
var localSourceRef: CGImageSourceRef = CGImageSourceCreateWithURL(refURL, nil)
var localMetadata: NSDictionary = CGImageSourceCopyPropertiesAtIndex(localSourceRef, 0, nil)
println("\n Photo data: \n")
println(localMetadata) 
}
ekostadinov
  • 6,880
  • 3
  • 29
  • 47
Cristian
  • 238
  • 1
  • 3
  • 13
  • UIImagePickerControllerReferenceURL will be nil in several cases, the most common being when coming from the camera (vs camera roll) because the image has not been saved yet. Can you add some additional info as to what test cases you've tried so far? – Matt S. Nov 07 '14 at 16:18
  • That's right, it works for the camera roll. But it crashes if the photo has been captured with the camera or if I try to access the photo from the Photo Library. Do you have any suggestion of how can I acees the gps data from the photo? Thank you! – Cristian Nov 08 '14 at 22:18

1 Answers1

2

So it sounds like there are actually two questions here:

1) Why is UIImagePickerControllerReferenceURL returning a nil reference?

2) How can you get location data from the photo?

So, the answer to (1) is usually because you receive the callback didFinishPickingMedia before the OS as written the file to the image library.

The answer to #2 is much trickier, as showcased by this question's line of answers: Reading the GPS data from the image returned by the camera in iOS iphone

There are a number of variables you need to account for:

  • iOS will strip the GPS data out if you haven't requested access to location data, so you'll need to prompt for location access using CLLocationManager.
  • If the user has geotagging disabled, you'll never get GPS coords.
  • If the phone can't get a GPS lock, iOS won't record the GPS coords.

Per this answer: https://stackoverflow.com/a/10338012/490180 you should be able to retrieve the raw UIImage and then create the CGImageSourceRef from the data property off of UIImage's CGImage. This effectively removes the need for you to ever access the ReferenceURL.

Community
  • 1
  • 1
Matt S.
  • 1,882
  • 13
  • 17
  • Tank you, I followed your advice and assigned the location to photo as you said. If you're willing to help, I have one more question here http://stackoverflow.com/questions/26760410/extract-gps-data-from-photo-swift – Cristian Nov 11 '14 at 11:59