经过一些艰苦的努力,我使用 CLLocationManager
将 GPS 数据注入(inject)到我的 UIImage EXIF 中。这真是一次糟糕的经历,我个人不推荐它。
我正在使用下面的代码将图像写入 Assets 库,并且在将其写入相机胶卷之前,我的元数据(对象#5,{GPS})的 NSDictionary 在断点写入处有一个日志输出:
Printing description of [5].key:
{GPS}
Printing description of [5].value:
{
Altitude = "-1.606232";
Latitude = "49.86973495227272";
LatitudeRef = N;
Longitude = "-125.1198035572889";
LongitudeRef = W;
}
保存方法如下:
- (void)saveToCameraRoll
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// Captured that NSDictionary excerpt from this next line on 'metadata' var
NSDictionary* metadata = [defaults objectForKey:kImageMetadata];
if (_imageToSave) {
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library writeImageToSavedPhotosAlbum:[_imageToSave CGImage] metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
// Darn, bad things happened
}
else
{
// Wahoo!
}
}];
}
}
并且定位位置更新委托(delegate)方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CGFloat currentLatitude = [manager location].coordinate.latitude;
CGFloat currentLongitude = [manager location].coordinate.longitude;
CGFloat currentAltitude = [manager location].altitude;
//For GPS Dictionary
if(!_locationData)
_locationData = [NSMutableDictionary dictionary];
[_locationData setValue:[NSNumber numberWithDouble:currentLatitude] forKey:(NSString*)kCGImagePropertyGPSLatitude];
[_locationData setValue:[NSNumber numberWithDouble:currentLongitude] forKey:(NSString*)kCGImagePropertyGPSLongitude];
NSString* ref;
if (currentLatitude <0.0)
ref = @"S";
else
ref =@"N";
[_locationData setValue:ref forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];
if (currentLongitude <0.0)
ref = @"W";
else
ref =@"E";
[_locationData setValue:ref forKey:(NSString*)kCGImagePropertyGPSLongitudeRef];
[_locationData setValue:[NSNumber numberWithFloat:currentAltitude] forKey:(NSString*)kCGImagePropertyGPSAltitude];
}
所以一切都很好,我查看保存的照片:纬度,一切都很好,但我查看经度,它是 0
。
我猜?在某个地方它不喜欢经度是负数。我不太了解地理坐标,但也许你知道,也许你愿意帮助我走出英吉利海峡?
最佳答案
万岁!
因为我声明了 S 和 W ref 值,所以我发现 EXIF 处理 GPS 数据的方式可能需要绝对值。
解决方案是不使用原始浮点值,而是使用 fabs()
来获取绝对浮点值。
...
[_locationData setValue:[NSNumber numberWithDouble:fabs(currentLatitude)] forKey:(NSString*)kCGImagePropertyGPSLatitude];
[_locationData setValue:[NSNumber numberWithDouble:fabs(currentLongitude)] forKey:(NSString*)kCGImagePropertyGPSLongitude];
...
关于objective-c - UIImage 元数据中的 iOS 位置数据不处理负 CGFloat/double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25074201/