使用 Photokit 编写带有元数据的照片

2023-12-05

我目前正在使用 ALAsset 框架将图像从照片库保存到带有元数据的文档目录。我使用的代码是

ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:[NSURL URLWithString:miv.assetURL] resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *image_representation = [asset defaultRepresentation];
        CGImageSourceRef source = Nil;
        uint8_t *buffer = (Byte*)malloc(image_representation.size);
        NSUInteger length = [image_representation getBytes:buffer fromOffset: 0.0  length:image_representation.size error:nil];
        if (length != 0)  {
          NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:image_representation.size freeWhenDone:YES];              
          NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:(id)[image_representation UTI] ,kCGImageSourceTypeIdentifierHint,nil];              
          // create CGImageSource with NSData
          source = CGImageSourceCreateWithData((__bridge CFDataRef) adata,  (__bridge CFDictionaryRef) sourceOptionsDict);              
        }
        NSDictionary *metadata = [image_representation metadata];
        NSMutableDictionary *metadataAsMutable = [metadata mutableCopy];
        NSMutableDictionary *EXIFDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary];
        NSMutableDictionary *GPSDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary];
        if(!EXIFDictionary) {
          EXIFDictionary = [NSMutableDictionary dictionary];
        }
        if(!GPSDictionary) {
          GPSDictionary = [NSMutableDictionary dictionary];
        }
        [metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
        [metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary];
        CFStringRef UTI = CGImageSourceGetType(source);
        NSMutableData *dest_data = [NSMutableData data];
        CGImageDestinationRef destination CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL);                        
        CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) metadataAsMutable);
        BOOL success = NO;
        success = CGImageDestinationFinalize(destination);

        if(!success) {
        }
        [dest_data writeToFile:myImageFileName atomically:YES];
        CFRelease(destination);
        CFRelease(source);
}];

我想转换此代码以使用 Photo Kit。根据我的研究,我发现照片套件处理写入的方式是

PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObjects:imageURLString, nil] options:nil];
[savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
[[PHImageManager defaultManager]
     requestImageForAsset:(PHAsset *)asset
     targetSize:desiredSize
     contentMode:PHImageContentModeAspectFit
     options:Nil
     resultHandler:^(UIImage *result, NSDictionary *info) {
         // Write Here
         // The info here has no EXIF or Metadata. So get them
         PHContentEditingInputRequestOptions *editOptions =      
         [[PHContentEditingInputRequestOptions alloc]init];
         editOptions.networkAccessAllowed = YES;
         [asset requestContentEditingInputWithOptions:editOptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
             CIImage *image = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];
             NSLog(@"metadata: %@", image.properties.description);
             ?? How do I write this to doc folder ?
       }];

     }
}

经过一番研究后我发现了这一点。以下是如何使用 Exif 将图像保存到文档目录。

PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObjects:myAssetURL, nil] options:nil];
        [savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
          PHImageRequestOptions *cropToSquare = [[PHImageRequestOptions alloc] init];
          cropToSquare.synchronous = YES;
          [[PHImageManager defaultManager] requestImageDataForAsset:asset
                                                            options:cropToSquare
                                                      resultHandler:
           ^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
             CIImage* ciImage = [CIImage imageWithData:imageData];
             NSMutableDictionary *metadataAsMutable = [ciImage.properties mutableCopy];
             CGImageSourceRef source = Nil;
             NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:dataUTI ,kCGImageSourceTypeIdentifierHint,nil];
             source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData,  (__bridge CFDictionaryRef) sourceOptionsDict);
             CFStringRef UTI = CGImageSourceGetType(source);
             NSMutableData *dest_data = [NSMutableData data];
             CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL);
             CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) metadataAsMutable);
             BOOL success = NO;
             success = CGImageDestinationFinalize(destination);

             if(!success) {
             }
             [dest_data writeToFile:myTrumbImageFileName atomically:YES];
             CFRelease(destination);
             CFRelease(source);
           }];
        }];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Photokit 编写带有元数据的照片 的相关文章

  • Swift - 我可能已经删除了 Apple Swift Packages 集合

    我对 swift 和 XCode 很陌生 昨天我正在开发一个项目 想尝试一下某人制作的自定义日期选择器 所以我转到 添加包 并粘贴 GitHub 链接并添加它 我已经在我的项目中添加了一个名为 KeychainAccess 的不同包 方式
  • iOS 9.3 中的 KVO 被破坏

    这可能是 iOS 9 3 发行版 中的一个可怕的错误 添加单个观察者时 NSUserDefaults standardUserDefaults 我注意到响应方法 observeValueForKeyPath ofObject change
  • React Native Detox:无法接受系统警报弹出的位置权限

    使用以下命令从命令行启动应用程序detox test configuration ios sim debug 通过代码启动应用程序await device launchApp permissions location always noti
  • 如何在 Alamofire 中使用“responseDecodable”方法?

    I have been trying to use responseDecodable method from Alamofire but I m getting Generic parameter T could not be infer
  • tableView:canEditRowAtIndexPath: 弹出 viewController 时崩溃

    我有一个带有UITableView 我允许通过滑动来编辑 删除 的行 就像在邮件应用程序中一样 我用以下方法来做到这一点 BOOL tableView UITableView tableView canEditRowAtIndexPath
  • 使用 HTTP NSURL 创建 AVAsset

    我正在尝试合并两个NSURLs包含视频参考 其中一个 URL 指向 AWS 上的视频 另一个 URL 指向本地存储的视频 我的导出代码有效 因为我已经尝试使用两个本地视频 但每当我尝试合并 HTTP url 和本地 url 时 我都会收到此
  • iOS UITableView:使用 CAGradientLayer 将背景颜色指定为渐变

    我刚刚开始使用 Xcode 4 3 中的主从视图模板 我试图更改主视图的背景颜色并将其设置为颜色渐变 这是我尝试过的 Colors m import Colors h implementation Colors UIColor naviga
  • 我的 iPhone 6 获取 iPhone 5 媒体查询

    我不明白这里发生了什么事 我在 CSS 媒体查询中专门针对 iphone 5 media only screen and min device width 320px and max device width 568px some div
  • XOR或其他简单的文件混淆方法ios目标c

    我正在开发一个使用受版权保护的 pdf 文件的 ios 应用程序 我正在寻找一些简单的方法来混淆文件以确保安全 这些方法不需要我经历额外的大众市场 CCATS 加密审查流程 但也将确保版权所有者已尽我所能来保证他们的数据安全 当然 我用密码
  • 如何在复杂的层次结构中上下同步 CALayer 和 UIView 动画

    See 如何在整个层次结构中管理 CALayer 动画 https stackoverflow com questions 26917908 how to manage calayer animations throughout a hie
  • FIRApp 链接器错误 [“_OBJC_CLASS_$_FIRApp”]

    我已经搜索过 SO 和 Google 但找不到有效的答案 我已经在多个项目中使用了新的 Firebase Cocoapod 但是现在 当将其添加到不同的项目时 我收到以下错误 我正在使用 Xcode 7 3 1 和 cocoapods 1
  • 辅助功能标识符在 iOS 模拟器的辅助功能检查器中不可见

    我想使用辅助功能检查器来验证在模拟器 iOS 9 2 中运行的应用程序中的所有辅助功能标识符 辅助功能检查器能够返回多个辅助功能字段 但不能返回标识符 知道为什么以及如何我能看到它们吗 实际上有一种方法 克里斯 普林斯 Chris Prin
  • Textview 中心文本对齐 IOS 7

    void observeValueForKeyPath NSString keyPath ofObject id object change NSDictionary change context void context NSLog He
  • 在 UIScrollView 中禁用 2 根手指滚动

    我想在我的应用程序中禁用两指滚动UIScrollView 我对其进行了子类化 并使用以下代码调整了其内置手势识别器 for UIGestureRecognizer mgestureRecognizer in scroller gesture
  • 使用后台配置时 NSURLSessionDownloadTask 不断重试

    当涉及到缓慢的后端和使用后台配置下载数据时 我遇到了问题 NSURLSessionConfiguration sessionConfig NSURLSessionConfiguration backgroundSessionConfigur
  • iOS 版 Google 地图 sdk 中折线的轮廓

    我的要求是在地图上显示一条绿色折线 但当地图切换到卫星视图时 绿色折线变得不清楚 我无法改变折线的颜色 因此 为了将折线与背景 地图的卫星视图 区分开来 我需要为折线绘制白色轮廓 我浏览了 GMSPolyline 类的文档 但找不到任何可以
  • 出现错误:FT_Open_Face 失败:错误 2

    当我使用时出现以下错误CGContextDrawPDFPage context PDFPage 对于某些文件 有解决办法来解决这个问题吗 FT Open Face failed error 2 错误2看起来像errno2 这是 找不到文件
  • ios swift 如何将默认语言设置为阿拉伯语?

    我正在开发一个有两种语言的应用程序 即英语和阿拉伯语 我用英语编写了该应用程序 然后用阿拉伯语本地化了该应用程序 更改语言时需要重新启动应用程序 我唯一的问题是 第一次安装应用程序时如何将默认语言设置为阿拉伯语 我尝试在编辑方案部分将语言设
  • 初始化Object中的空字符串?

    有人使用以下方法来初始化 NSstring NSString astring NSString alloc init 我想知道为什么不直接使用 NSString atring nil or NSString astring 没有semant
  • 在启动屏幕中执行代码已更新

    在原始启动屏幕中执行代码 https stackoverflow com questions 27642016 execute code in launch screen 现在默认的LaunchScreenXcode 项目中的文件已更改为

随机推荐