【发布时间】: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