将图像元数据(EXIF/TIFF/IPTC)写入OS X中的图像文件 [英] Writing image metadata (EXIF/TIFF/IPTC) to image file in OS X

查看:342
本文介绍了将图像元数据(EXIF/TIFF/IPTC)写入OS X中的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个照片编辑应用程序,到目前为止,我已经成功地从图像文件中读取了元数据(在得到以下问题的答案后:

I am creating a photo editing app, and so far I've managed to read the metadata from image files successfully (after getting an answer to this question: 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是从初始图像读取的字典):

This copies all the metadata of the image file to a dictionary, and it works faily well. However, I couldn't find out how to write this metadata back to a newly created NSImage (or to an image file). Here is how I save my file (where img is an NSImage instance without metadata and self.cachedMetadata is the dictionary read from the initial image):

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-only),但是因为EXIF缺少一些初始图像具有的字段(IPTC和TIFF标签),所以我需要更改阅读方法.现在我拥有所有数据,但是我不知道如何将其写入到新创建的图像文件中.

How can I write the metadata? I used to write just EXIF for JPEG (the dictionary was EXIF-only previously) successfully but because EXIF lacked some of the fields that the initial images had (IPTC and TIFF tags) I needed to change my reading method. Now I have all the data, but I don't know how to write it to the newly-created image file.

谢谢, 可以.

推荐答案

找到了另一个StackOverflow问题的答案:

Found an answer from another StackOverflow question: How do you overwrite image metadata?:

(从该问题本身获取并针对我的需要进行修改的代码,其中包含我的问题的答案)

(code taken from that question itself and modified for my needs, which contains the answer to my question)

//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.

This worked perfectly for writing back all the metadata including EXIF, TIFF, and IPTC.

这篇关于将图像元数据(EXIF/TIFF/IPTC)写入OS X中的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆