您应该首先从文件中读取元数据:
CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
NSDictionary *props = (__bridge_transfer NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
在上面的代码中,url
是图像文件的 URL。props
将在目标 URL 中包含图像的所有元数据。
然后,将该数据复制到新的可变的空数据源:
//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
在此示例中):
//copy image data
CGImageDestinationAddImageFromSource(imgDest, source, 0, (__bridge CFDictionaryRef)(modifiedMetadata));
BOOL success = CGImageDestinationFinalize(imgDest);
这会将元数据写入目标图像。至少就我而言,它工作得很好。
要将图像数据保存到实际文件中,您可以定期写入数据,例如:
[resultData writeToFile:fileName atomically:YES];