0
votes

I am attempting to get the GPS data from an image that was picked from a UIImagePickerController

I am using this site as a reference.

After the the pick the selected image, the CGImageSourceRef is always null.

Here is the output from my NSLog

Info: { UIImagePickerControllerMediaType = "public.image"; UIImagePickerControllerOriginalImage = " size {2448, 3264} orientation 3 scale 1.000000"; UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=B01DF2C7-B954-41D8-B3FE-6096706DF3BB&ext=JPG"; }

imageFileURL: assets-library://asset/asset.JPG?id=B01DF2C7-B954-41D8-B3FE-6096706DF3BB&ext=JPG

ImageSource was Null

Here is my code:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSLog(@"Info: %@", info.description);


    [picker dismissViewControllerAnimated:YES completion:^{


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            NSURL *imageFileURL = (NSURL*) info[UIImagePickerControllerReferenceURL];

            NSLog(@"imageFileURL: %@", imageFileURL.description);

            CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef) imageFileURL, NULL);

            if (imageSource == NULL) {
                NSLog(@"ImageSource was Null");
                return;
            }

            NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
                                    nil];
            CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
            CFRelease(imageSource);

            CFDictionaryRef gps = CFDictionaryGetValue(imageProperties, kCGImagePropertyGPSDictionary);
            if (gps) {
                NSString *latitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitude);
                NSString *latitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitudeRef);
                NSString *longitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitude);
                NSString *longitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitudeRef);
                NSLog(@"GPS Coordinates: %@ %@ / %@ %@", longitudeString, longitudeRef, latitudeString, latitudeRef);
            }

            dispatch_async(dispatch_get_main_queue(), ^{

            }); //distpach get main queue
        });//background queueu
    }];//picker completion

}

I am not sure what I am doing wrong. Any help?

1

1 Answers

0
votes

UIImagePickerControllerReferenceURL is deprecated. You should use UIImagePickerControllerPHAsset instead. Assuming there is a PHAsset for that key in the info dictionary, it should also provide location data as a CLLocation.

Edit: Since you said you need to support earlier OS versions, this code worked for me based on what you already have.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  [picker dismissViewControllerAnimated:YES completion:^{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      NSURL *imageFileURL = info[UIImagePickerControllerImageURL];
      CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageFileURL, NULL);

      if (imageSource == NULL) {
        NSLog(@"ImageSource was Null");
        return;
      }

      NSDictionary *options = @{(NSString *)kCGImageSourceShouldCache : @(NO)};
      CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
      CFRelease(imageSource);

      CFDictionaryRef gps = CFDictionaryGetValue(imageProperties, kCGImagePropertyGPSDictionary);
      NSString *latitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitude);
      NSString *latitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitudeRef);
      NSString *longitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitude);
      NSString *longitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitudeRef);
      NSLog(@"GPS Coordinates: %@ %@ / %@ %@", longitudeString, longitudeRef, latitudeString, latitudeRef);
    });//background queue
  }];//picker completion
}