Write IPTC data to file

梦想与她 提交于 2019-12-01 00:49:16

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);

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);

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];

you should use XMP Toolkit SDK, you can find detailed information at adobe SDK page implementing is little bit tricky but once you add static libraries to your project you can read and write XMP information, it covers IPTC namespace as well upon others namespaces as like dublin-core etc.

After you add libraries to project code like this.

#include "XMP.incl_cpp"
#include "XMP.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>

-(void)readIPTC
{
    SXMPMeta::Initialize()
    SXMPMeta imageMeta;

    if(imageMeta.DoesPropertyExist(kXMP_NS_DC, "title"))
    {
        std::string MyString;
        imageMeta.GetArrayItem(kXMP_NS_DC, "title", 1, &MyString, 0);
        [textField setStringValue:[NSString stringWithCString:MyString.c_str() encoding:[NSString defaultCStringEncoding]]];
    }
}

writing is pretty much same.

imageMeta.SetProperty(<#XMP_StringPtr schemaNS#>, <#XMP_StringPtr propName#>, <#XMP_StringPtr propValue#>)

you can find all namespace constants in documentation as like kXMP_NS_DC XMP NameSpace DublinCore etc.

Apple's Image/IO suppose to cover all but for instance you can read byline entry from IPTC with Image/IO however you can't write it.

Image/IO Reference

CGImageProperties Reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!