使用 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 包含所有相关内容。不幸的是,Not Working Meta 正在输出 null。据我所知,我需要增强图像源和 CFDictionary 的选项,而不是使用 nil。我在正确的道路上吗?如果是这样,我应该做些什么改变来提取 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);
此时变量data
应该包含所有信息(图像+GPS元数据+其他元数据),你可以使用这个data
来存储任何你喜欢的地方。
编辑:
要在您的类中添加核心位置功能,请在其中声明 CLLocationManagerDelegate 协议(protocol)
标题。在 viewDidLoad
中实例化一个位置管理器:
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)locationManagerCLLocationManager*)manager didUpdateLocationsNSArray*)locations {
if (!locations || [locations count] == 0) {
return;
}
CLLocation* loc = locations[[locations count] - 1];
if (loc.horizontalAccuracy < 0) {
return;
}
self.currentLocation = loc;
}
-(void)locationManagerCLLocationManager*)manager didUpdateToLocationCLLocation*)newLocation fromLocationCLLocation*)oldLocation {
if (newLocation.horizontalAccuracy < 0) {
return;
}
self.currentLocation = newLocation;
}
-(void)locationManagerCLLocationManager*)manager didFailWithErrorNSError*)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
作为你的 `location 变量。
EDIT2
好吧,您似乎已经在使用来自 GusUtils 的“NSMutableDictionary+ImageMetadata.h”类别。所以我对我的答案做了一些修改。请试一试。
关于ios - 试图从 UIImagePickerController 中提取 GPS 元数据,但 NSLog 显示为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20105571/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |