【问题标题】:Writing image metadata (EXIF/TIFF/IPTC) to image file in OS X在 OS X 中将图像元数据 (EXIF/TIFF/IPTC) 写入图像文件
【发布时间】:2013-11-10 15:05:28
【问题描述】:

我正在创建一个照片编辑应用程序,到目前为止,我已经成功地从图像文件中读取元数据(在得到这个问题的答案后:Reading Camera data from EXIF while opening NSImage on OS X)。

source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
NSDictionary *props = (__bridge_transfer NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);

这会将图像文件的所有元数据复制到字典中,但效果不佳。但是,我不知道如何将此元数据写回新创建的NSImage(或图像文件)。这是我保存文件的方式(其中img 是一个NSImage 实例没有 元数据,self.cachedMetadata 是从初始图像读取的字典):

NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[img TIFFRepresentation]];
[rep setProperty:NSImageEXIFData withValue:self.cachedMetadata];
NSData *data;
if([[fileName lowercaseString] rangeOfString:@".png"].location != NSNotFound){
    data = [rep representationUsingType:NSPNGFileType properties:nil];
}else if([[fileName lowercaseString] rangeOfString:@".tif"].location != NSNotFound){
    data = [rep representationUsingType:NSTIFFFileType properties:nil];
}else{ //assume jpeg
    data = [rep representationUsingType:NSJPEGFileType properties:@{NSImageCompressionFactor: [NSNumber numberWithFloat:1], NSImageEXIFData: self.cachedMetadata}];
}

[data writeToFile:fileName atomically:YES];

如何编写元数据?我曾经成功地为 JPEG 编写了 EXIF(该字典以前只有 EXIF),但由于 EXIF 缺少初始图像所具有的一些字段(IPTC 和 TIFF 标签),我需要更改我的阅读方法。现在我有了所有的数据,但我不知道如何将它写入新创建的图像文件。

谢谢, 可以。

【问题讨论】:

  • 你好!我或多或少处于相同的情况,但无法理解您的解决方案。我有关键字、标题和描述的列表,我必须写到我知道路径的 jpg 的 IPTC 部分。如何才能做到这一点?你能看看这个问题,也许发布一个答案? stackoverflow.com/questions/23874865/write-iptc-data-to-file

标签: objective-c macos exif nsimage iptc


【解决方案1】:

从另一个 StackOverflow 问题中找到答案:How do you overwrite image metadata?

(取自该问题本身并根据我的需要进行修改的代码,其中包含我的问题的答案)

//assuming I've already loaded the image source and read the meta
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL);
CFStringRef imageType = CGImageSourceGetType(source);

//new empty data to write the final image data to
NSMutableData *resultData = [NSMutableData data];
CGImageDestinationRef imgDest = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)(resultData), imageType, 1, NULL);

//copy image data
CGImageDestinationAddImageFromSource(imgDest, source, 0, (__bridge CFDictionaryRef)(window.cachedMetadata));
BOOL success = CGImageDestinationFinalize(imgDest);

这非常适合写回所有元数据,包括 EXIF、TIFF 和 IPTC。

【讨论】:

  • 这是否也会保留剪切路径(如果存在)?
  • 我在这里提出了一个新问题:stackoverflow.com/questions/32160528/… 今晚晚些时候将添加 Bounty。如果您有自己的照片编辑软件,可能对您也很感兴趣。
  • 在我发布的链接中,还有一个带有剪切路径的示例 JPG。你能看看吗?我会给解决它的人另一个赏金。暂时无法打开赏金,因为如果在 7 天内没有答案,它就消失了......谢谢
【解决方案2】:

您可以尝试使用或查看“exiv2”工具中的代码。

【讨论】:

  • 我有一个商业项目,商业许可证需要联系创作者。我最好坚持使用内置解决方案。有一种方法可以完全提取所有数据,也必须有一种方法可以将所有数据写回到图像中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 2019-10-06
  • 1970-01-01
相关资源
最近更新 更多