objective-c - UIImage 元数据中的 iOS 位置数据不处理负 CGFloat/double

标签 objective-c uiimage cllocationmanager

经过一些艰苦的努力,我使用 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/

相关文章:

ios - 调整 UIImage 的大小 - 性能问题

iphone - 如何检查 iOS 4.2 之前的特定应用程序是否启用了位置服务?

ios - Swift 3.0 允许权限警报不间断

ios - 为什么在 XCode 调试器中使用 "Step over"功能会隐藏本应强大的弱属性的问题

ios - 核心数据 : Fetch Multiple Entities using NSFetchedResultController

ios - UIImageView 上的 UIGestureRecognizer

objective-c - NSTimer 在 OS X(OS X 10.9) 上不正确

ios - 使用可调整大小的 UIImage 蒙版屏蔽任意大小的 UIImageView

iphone - UIImagePickerController - 从应用程序文档目录保存和检索照片

iphone - 检查 locationServicesEnabled 始终返回 YES,无论是否手动切换开关来决定是否启用位置服务