我正在创建一个照片编辑应用程序,到目前为止,我已经成功地从图像文件中读取元数据(在得到这个问题的答案后:在 OS X 上打开 NSImage 时从 EXIF 读取相机数据).
source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
NSDictionary *props = (__bridge_transfer NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
这会将图像文件的所有元数据复制到字典中,但效果不佳。但是,我不知道如何将此元数据写回新创建的NSImage
(或图像文件)。这是我保存文件的方法(其中img
是一个 NSImage 实例without元数据和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 标签),我需要更改我的读取方法。现在我有了所有数据,但我不知道如何将其写入新创建的图像文件。
谢谢,
能。
从另一个 StackOverflow 问题中找到答案:如何覆盖图像元数据?:
(代码取自该问题本身并根据我的需要进行修改,其中包含我的问题的答案)
//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。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)