尝试从UIImagePickerController拉动GPS元数据,但NSLog显示为空

使用UIImagePickerController我试图从图像的元数据收集GPS信息。 这是我的代码示例:

NSDictionary *imageMetadata = [info objectForKey: UIImagePickerControllerMediaMetadata]; NSLog(@"Working META: %@",imageMetadata); CGDataProviderRef dataProvider = CGImageGetDataProvider(originalImage.CGImage); CFDataRef data = CGDataProviderCopyData(dataProvider); CGImageSourceRef imageSource = CGImageSourceCreateWithData(data, nil); NSDictionary *metaDict = (__bridge NSDictionary *)(CGImageSourceCopyProperties(imageSource, nil)); NSLog(@"Not WOrking META: %@",metaDict); 

在NSLog的输出中,我可以看到Working Meta包含所有关联的内容。 不幸的是,不工作元输出空。 从我可以告诉我需要增强图像源和CFDictionary上的选项,而不是使用零。 我在正确的道路上? 如果是这样,我应该做些什么来拉动GPS数据?

尝试获取源EXIF词典的可变副本,如下所示:

 UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; NSData *jpeg = UIImageJPEGRepresentation(image,image.scale); CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)jpeg, NULL); NSDictionary *metadata = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL); NSMutableDictionary *mutableMetadata = [metadata mutableCopy]; 

现在将GPS数据设置为EXIF字典(代码GusUtils提供 ):

 [mutableMetadata setLocation:self.currentLocation] 

最后你需要将这些数据填充到某个目的地

 CFStringRef UTI = CGImageSourceGetType(source); NSMutableData *data = [NSMutableData data]; CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef) data, UTI, 1, NULL); CGImageDestinationAddImageFromSource(destination,source, 0, (__bridge CFDictionaryRef) mutableMetadata); BOOL success = CGImageDestinationFinalize(destination); 

此时,variablesdata应该包含所有信息(图像+ GPS元数据+其他元数据),您可以使用这些data存储在任何你喜欢的地方。

编辑:

在您的类中添加核心位置function在头中声明CLLocationManagerDelegate协议。 在您的viewDidLoad实例化一个位置pipe理器:

 self.locManager = [CLLocationManager new]; self.locManager.delegate = self; self.locManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; self.locManager.distanceFilter = 5.0f; //location would be updated if moved by 5 miter [self.locManager startUpdatingLocation]; 

并执行以下方法收集位置信息:

 -(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations { if (!locations || [locations count] == 0) { return; } CLLocation* loc = locations[[locations count] - 1]; if (loc.horizontalAccuracy < 0) { return; } self.currentLocation = loc; } -(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation { if (newLocation.horizontalAccuracy < 0) { return; } self.currentLocation = newLocation; } -(void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error { NSLog(@"core location error: %@",[error localizedDescription]); if ([error code] != kCLErrorLocationUnknown) { NSLog(@"location service will terminate now"); self.locManager.delegate = nil; [self.locManager stopUpdatingLocation]; self.currentLocation = nil; } } 

还要确保在离开VC之前停止服务

 self.locManager.delegate = nil; [self.locManager stopUpdatingLocation]; 

然后你可以使用self.currentLocation作为你的位置variables。

EDIT2

好吧,似乎你已经在使用GusUtils的“NSMutableDictionary + ImageMetadata.h”类。 所以我对我的答案做了一些修改。 请试一试。