将 UIImage 与元数据(EXIF、GPS、TIFF)一起写入 iPhone 的照片库中

2024-01-13

我正在开发一个项目,其要求是: - 用户将通过应用程序打开相机 - 捕获图像后,一些数据将附加到捕获图像的元数据中。 我浏览过一些论坛。我尝试编写这个逻辑。我想,我已经达到了这一点,但是缺少一些东西,因为我无法看到附加到图像的元数据。 我的代码是:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)dictionary 
{

    [picker dismissModalViewControllerAnimated:YES];

    NSData *dataOfImageFromGallery = UIImageJPEGRepresentation (image,0.5);
    NSLog(@"Image length:  %d", [dataOfImageFromGallery length]);


    CGImageSourceRef source;
    source = CGImageSourceCreateWithData((CFDataRef)dataOfImageFromGallery, NULL);

    NSDictionary *metadata = (NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);

    NSMutableDictionary *metadataAsMutable = [[metadata mutableCopy]autorelease];
    [metadata release];

    NSMutableDictionary *EXIFDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary]mutableCopy]autorelease];
    NSMutableDictionary *GPSDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary]mutableCopy]autorelease];


    if(!EXIFDictionary) 
    {
        //if the image does not have an EXIF dictionary (not all images do), then create one for us to use
        EXIFDictionary = [NSMutableDictionary dictionary];
    }

    if(!GPSDictionary) 
    {
        GPSDictionary = [NSMutableDictionary dictionary];
    }

    //Setup GPS dict - 
    //I am appending my custom data just to test the logic……..

    [GPSDictionary setValue:[NSNumber numberWithFloat:1.1] forKey:(NSString*)kCGImagePropertyGPSLatitude];
    [GPSDictionary setValue:[NSNumber numberWithFloat:2.2] forKey:(NSString*)kCGImagePropertyGPSLongitude];
    [GPSDictionary setValue:@"lat_ref" forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];
    [GPSDictionary setValue:@"lon_ref" forKey:(NSString*)kCGImagePropertyGPSLongitudeRef];
    [GPSDictionary setValue:[NSNumber numberWithFloat:3.3] forKey:(NSString*)kCGImagePropertyGPSAltitude];
    [GPSDictionary setValue:[NSNumber numberWithShort:4.4] forKey:(NSString*)kCGImagePropertyGPSAltitudeRef]; 
    [GPSDictionary setValue:[NSNumber numberWithFloat:5.5] forKey:(NSString*)kCGImagePropertyGPSImgDirection];
    [GPSDictionary setValue:@"_headingRef" forKey:(NSString*)kCGImagePropertyGPSImgDirectionRef];

    [EXIFDictionary setValue:@"xml_user_comment" forKey:(NSString *)kCGImagePropertyExifUserComment];
    //add our modified EXIF data back into the image’s metadata
    [metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
    [metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary];

    CFStringRef UTI = CGImageSourceGetType(source);
    NSMutableData *dest_data = [NSMutableData data];

    CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef) dest_data, UTI, 1, NULL);

    if(!destination)
    {
        NSLog(@"--------- Could not create image destination---------");
    }


    CGImageDestinationAddImageFromSource(destination, source, 0, (CFDictionaryRef) metadataAsMutable);

    BOOL success = NO;
    success = CGImageDestinationFinalize(destination);

    if(!success)
    {
        NSLog(@"-------- could not create data from image destination----------");
    }

    UIImage * image1 = [[UIImage alloc] initWithData:dest_data];
    UIImageWriteToSavedPhotosAlbum (image1, self, nil, nil);    
}

请帮助我做到这一点并得到一些积极的东西。 看最后一行,我是否保存了带有元数据的图像? 此时图像已保存,但我附加到图像的元数据并未保存。

提前致谢。


Apple 更新了解决此问题的文章(技术问答 QA1622)。如果您使用的是较旧版本的 Xcode,您可能仍然会看到这篇文章,或多或少,运气不好,如果不对图像数据进行低级解析,您将无法做到这一点。

https://developer.apple.com/library/ios/#qa/qa1622/_index.html https://developer.apple.com/library/ios/#qa/qa1622/_index.html

我在那里修改了代码如下:

- (void) saveImage:(UIImage *)imageToSave withInfo:(NSDictionary *)info
{
    // Get the assets library
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    // Get the image metadata (EXIF & TIFF)
    NSMutableDictionary * imageMetadata = [[info objectForKey:UIImagePickerControllerMediaMetadata] mutableCopy];

    // add GPS data
    CLLocation * loc = <•••>; // need a location here
    if ( loc ) {
        [imageMetadata setObject:[self gpsDictionaryForLocation:loc] forKey:(NSString*)kCGImagePropertyGPSDictionary];
    }

    ALAssetsLibraryWriteImageCompletionBlock imageWriteCompletionBlock =
    ^(NSURL *newURL, NSError *error) {
        if (error) {
            NSLog( @"Error writing image with metadata to Photo Library: %@", error );
        } else {
            NSLog( @"Wrote image %@ with metadata %@ to Photo Library",newURL,imageMetadata);
        }
    };

    // Save the new image to the Camera Roll
    [library writeImageToSavedPhotosAlbum:[imageToSave CGImage] 
                                 metadata:imageMetadata 
                          completionBlock:imageWriteCompletionBlock];
    [imageMetadata release];
    [library release];
}

我把它称为

imagePickerController:didFinishPickingMediaWithInfo:

这是图像选择器的委托方法。

我使用辅助方法(改编自GusUtils https://github.com/gpambrozio/GusUtils) 从某个位置构建 GPS 元数据字典:

- (NSDictionary *) gpsDictionaryForLocation:(CLLocation *)location
{
    CLLocationDegrees exifLatitude  = location.coordinate.latitude;
    CLLocationDegrees exifLongitude = location.coordinate.longitude;

    NSString * latRef;
    NSString * longRef;
    if (exifLatitude < 0.0) {
        exifLatitude = exifLatitude * -1.0f;
        latRef = @"S";
    } else {
        latRef = @"N";
    }

    if (exifLongitude < 0.0) {
        exifLongitude = exifLongitude * -1.0f;
        longRef = @"W";
    } else {
        longRef = @"E";
    }

    NSMutableDictionary *locDict = [[NSMutableDictionary alloc] init];

    [locDict setObject:location.timestamp forKey:(NSString*)kCGImagePropertyGPSTimeStamp];
    [locDict setObject:latRef forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];
    [locDict setObject:[NSNumber numberWithFloat:exifLatitude] forKey:(NSString *)kCGImagePropertyGPSLatitude];
    [locDict setObject:longRef forKey:(NSString*)kCGImagePropertyGPSLongitudeRef];
    [locDict setObject:[NSNumber numberWithFloat:exifLongitude] forKey:(NSString *)kCGImagePropertyGPSLongitude];
    [locDict setObject:[NSNumber numberWithFloat:location.horizontalAccuracy] forKey:(NSString*)kCGImagePropertyGPSDOP];
    [locDict setObject:[NSNumber numberWithFloat:location.altitude] forKey:(NSString*)kCGImagePropertyGPSAltitude];

    return [locDict autorelease];

}

到目前为止,这对我来说在 iOS4 和 iOS5 设备上运行良好。

Update:和 iOS6/iOS7 设备。我使用以下代码构建了一个简单的项目:

https://github.com/5teev/MetaPhotoSave https://github.com/5teev/MetaPhotoSave

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 UIImage 与元数据(EXIF、GPS、TIFF)一起写入 iPhone 的照片库中 的相关文章

  • 如何检查 BOOL 是否为空?

    有没有办法在将值分配给 BOOL 之前检查该值是否为 NULL Nil 例如 我在 NSDictionary 中有一个值可以是 TRUE FALSE NULL mySTUser current user following results
  • 使用 NSDateFormatter 从字符串中获取日期,无论 12 小时到 24 小时设置如何

    今天我的问题是关于日期格式和字符串 我的应用程序从互联网下载一些表示日期的字符串 日期格式始终是这样的 2010 05 24 at 20 45 我需要将此字符串转换为 NSDate 对象才能执行一些日期操作 我尝试了这段代码 NSStrin
  • 获取对 iOS 应用程序中最顶层视图/窗口的引用

    我正在创建一个可重用的框架 用于在 iOS 应用程序中显示通知 我希望将通知视图添加到应用程序中其他所有内容的顶部 有点像 UIAlertView 当我初始化监听 NSNotification 事件并添加视图作为响应的管理器时 我需要获取对
  • iphone:在 UIWebView 之上查看?

    我正在开发一个浏览器应用程序 并且 UIWebView 顶部有一个地址栏 在 MobileSafari 上 如果向下滚动 地址栏开始移动到顶部 移出屏幕 并且 UIWebView 不会滚动 只有当地址栏完全消失时 它才开始滚动 我也想在我的
  • 两者都实现了类。将使用两者之一

    我有一个项目 它具有使用 SocketRocket 的依赖项 通过 CocoaPods 安装 并从 HeapAnalytics 导入了静态库 显然 HeapAnalytics 库已经使用了 SocketRocket 编译时没有出现错误 但在
  • 多次添加同一个子视图来查看

    我不知道这是否可行 但我想做的是将子视图多次添加到视图中 我尝试过这样的事情 self view addSubview newView newView center CGPointMake 160 100 self view addSubv
  • iPhone 崩溃日志?

    我已经配置了一部 iPhone 并让用户安装了该应用程序 它失败 是否有崩溃日志可以让我看到 iPhone 上失败的原因 Ian 如果您可以使用 xcode 将 iPhone 连接到计算机 则在管理器窗口中它会显示每个应用程序崩溃的崩溃日志
  • 用 UIView 像翻书一样翻页?

    我正在尝试在之间切换UIViews让它看起来就像你正在翻书的一页 The UIViewAnimationTransitionCurlUp如果我能让它向左或向右卷曲 那就非常接近了 这可能吗 我尝试过使用CATRansition但没有一种动画
  • 使用超类初始化器初始化类

    我有两个类 一个是另一个的子类 比如说Animal and Dog 超类有一些初始化器 比如initAnimal 子类有一些初始化器 比如initDog 问题是 从编译器的角度来看 做类似的事情是完全合法的Dog adog Dog allo
  • iOS中的performSelector有什么用

    的作用是什么执行选择器 比较 self btnClicked and self performSelector selector btnClicked void btnClicked NSLog Method Called 两者都对我来说工
  • UICollectionView 未出现

    我正在尝试设置UICollectionView 以编程方式在我的视图控制器中扩展UIViewController 由于某种原因 我的收藏视图根本没有显示 以下是我所拥有的 为什么没有出现 我将它连接到委托和数据源并将其添加为子视图self
  • 沿着预定路径移动图像?

    是否可以通过按下 iphone SDK 中的按钮来将图像设置为沿着预定路径运动 我不是在寻找任何奇特的东西 我正在研究一个简单的概念 但这会节省大量动画工作 是的 您可以通过创建一个路径来为任何 CALayer 制作动画CAKeyframe
  • 您将如何实现一个检测水平滑动以允许分页的 UITableView?

    我想实现一个 UITableView 一次显示 20 行 但考虑到我实际上可能要展示 120 个项目 我想使用某种分页来处理这个问题 使用前 20 项填充表 当用户从右向左滑动时 用接下来的 20 个项目重新加载 UITableView 从
  • 使用 Google place API 从 lat long 获取附近的地点

    我正在使用 google place API 即 https maps googleapis com maps api place search json location 33 7167 73 0667 radius 500 type f
  • Xcode 3.1.4 中内置分析器

    我想知道 Xcode 3 1 4 中内置的分析器是否使得单独使用 LLVM Clang 静态分析器变得多余 请参考这里的原文 使用 LLVM Clang 静态分析器查找内存泄漏 http www fruitstandsoftware com
  • iPhone - 如何在矩形中间绘制文本

    有没有一种方法可以在矩形中间绘制文本 我可以找到各种对齐方式 但我尝试过的任何方法都不能将文本垂直居中在矩形中 有没有一种简单的方法可以做到这一点 或者有什么方法可以将矩形居中然后在其中绘制 我直接绘制到 CGContext 尝试使用 NS
  • 我什么时候应该对 IBOutlet 使用弱或强限定符? [复制]

    这个问题在这里已经有答案了 可能的重复 ARC 下 IBOutlets 应该强还是弱 https stackoverflow com questions 7678469 should iboutlets be strong or weak
  • 将 CALayer 旋转 90 度?

    如何旋转CALayer90度 我需要旋转所有内容 包括子图层和坐标系 Obj C theLayer transform CATransform3DMakeRotation 90 0 180 0 M PI 0 0 0 0 1 0 Swift
  • 我应该在哪个方法中设置 UITextField 的委托?

    在 viewDidLoad 或 init 方法中设置 UITextField 的委托是一个好习惯吗 我尝试在 init 方法中将委托设置为 self 但它没有调用相应的委托方法 当我将代码移动到 viewDidLoad 中时 它注册为将 s
  • UINavigationController 在 UITabBarController 的 moreNavigationController 中不起作用

    我正在处理UINavigationControllers在我的应用程序中 全部由UITabBarController 一切工作正常 直到我的控制器落入自动生成的 更多 选项卡 我在简单的示例中重现了该问题 难道我做错了什么 我想不通 感谢您

随机推荐