将IPTC数据写入文件 [英] Write IPTC data to file

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

问题描述

我需要获取一个现有的jpg文件,并修改其IPTC条目中的标题,说明和关键字。这里有几个主题,但是所有主题都没有答案或只有部分答案。我已经知道如何阅读IPTC信息,但是需要对其进行编辑。有人可以对这个研究较少且鲜为人知的话题有所了解吗?

I would need to take an existing jpg file and modify the title, the description and the keywords in its IPTC entries. There are several topics here on this but all either without answer or with partial answers. I already know how to read the IPTC informations, but would need to edit them. Could somebody shed some light on this much researched and less known topic?

我所拥有的是:

NSString: title
NSString: description
NSArray: keywords
NSString: path to the file

我想使用现有IPTC数据拍摄现有图像,并用这些数据替换现有条目,但保留所有其他IPTC条目,例如位置,日期等。到目前为止,我所知道的就是我需要使用CGImageDestination。

I would like to take an existing image with existing IPTC data and replace the existing entries with these, but preserve all other IPTC entries such as location, date and so on. All I know so far is that i need to use CGImageDestination.

谢谢

推荐答案

您应该首先从文件中读取元数据:

You should first read the metadata from the file:

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

在上面的代码中, url 是图片文件的网址。 道具将把图像的所有元数据放在目标URL。

In the code above, url is the URL of your image file. props will have all the metadata of the image at the destination URL.

然后,将数据复制到新的可变的空数据源:

Then, you copy that data to a new mutable, empty data source:

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

最后,假设您已经在新的 NSDictionary 实例(在此示例中称为 modifiedMetadata ):

Finally, let's assume you've modified the metadata in a new NSDictionary instance (called modifiedMetadata in this example):

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

这会将元数据写入目标图像。

This would write the metadata to the destination image. At least in my case, it works perfectly.

要将图像数据保存到实际文件中,可以定期写入数据,例如:

To save the image data to an actual file, you can write the data regularly, e.g:

[resultData writeToFile:fileName atomically:YES];

这篇关于将IPTC数据写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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