我正在创建照片编辑应用程序,到目前为止,我已成功读取图像文件中的元数据(获得此问题的答案:Reading Camera data from EXIF while opening NSImage on OS X)。在OS X中将图像元数据(EXIF/TIFF/IPTC)写入图像文件
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标签)的字段,所以我需要更改我的阅读方法。现在我拥有所有的数据,但我不知道如何将它写入新创建的图像文件。
谢谢, 可以。
你好!我或多或少处于相同的情况,但无法理解您的解决方案。我有关键字,标题和说明,我必须写信给IPTC部分的我知道路径。如何才能做到这一点?你可以看看这个问题,也许发表一个答案? http://stackoverflow.com/questions/23874865/write-iptc-data-to-file – user2452250