Reading the GPS data from the image returned by the camera in iOS iphone

后端 未结 9 1293
余生分开走
余生分开走 2020-11-29 21:36

I need to get the GPS coordinates of an image taken with the iOS device\'s camera. I do not care about the Camera Roll images, just the image taken with UIImagePickerControl

9条回答
  •  -上瘾入骨i
    2020-11-29 22:10

    As point out by Chris Markle, Apple does strip out GPS data from EXIF. But you can open the RAW data of the image, and parse the data yourself or use a third party lib to do that, for example.

    Here is a sample code:

    - (void) imagePickerController: (UIImagePickerController *) picker
     didFinishPickingMediaWithInfo: (NSDictionary *) info {
    
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL]
                 resultBlock:^(ALAsset *asset) {
    
                     ALAssetRepresentation *image_representation = [asset defaultRepresentation];
                     NSUInteger size = (NSUInteger)image_representation.size;
                     // create a buffer to hold image data
                     uint8_t *buffer = (Byte*)malloc(size);
                     NSUInteger length = [image_representation getBytes:buffer fromOffset: 0.0  length:size error:nil];
    
                     if (length != 0)  {
    
                         // buffer -> NSData object; free buffer afterwards
                         NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:size freeWhenDone:YES];
    
                         EXFJpeg* jpegScanner = [[EXFJpeg alloc] init];
                         [jpegScanner scanImageData: adata];
                         EXFMetaData* exifData = jpegScanner.exifMetaData;
    
                         id latitudeValue = [exifData tagValue:[NSNumber numberWithInt:EXIF_GPSLatitude]];
                         id longitudeValue = [exifData tagValue:[NSNumber numberWithInt:EXIF_GPSLongitude]];
                         id datetime = [exifData tagValue:[NSNumber numberWithInt:EXIF_DateTime]];
                         id t = [exifData tagValue:[NSNumber numberWithInt:EXIF_Model]];
    
                         self.locationLabel.text = [NSString stringWithFormat:@"Local: %@ - %@",latitudeValue,longitudeValue];
                         self.dateLavel.text = [NSString stringWithFormat:@"Data: %@", datetime];
    
                     }
                     else {
                         NSLog(@"image_representation buffer length == 0");
                     }
                 }
                failureBlock:^(NSError *error) {
                    NSLog(@"couldn't get asset: %@", error);
                }
         ];
    }
    

提交回复
热议问题