根据苹果公司的
Image I/O编程指南,您可以使用
CGImageDestinationSetProperties
来添加属性的
CFDictionaryRef
。
他们的示例代码如下:
float compression = 1.0; // Lossless compression if available.
int orientation = 4; // Origin is at bottom, left.
CFStringRef myKeys[3];
CFTypeRef myValues[3];
CFDictionaryRef myOptions = NULL;
myKeys[0] = kCGImagePropertyOrientation;
myValues[0] = CFNumberCreate(NULL, kCFNumberIntType, &orientation);
myKeys[1] = kCGImagePropertyHasAlpha;
myValues[1] = kCFBooleanTrue;
myKeys[2] = kCGImageDestinationLossyCompressionQuality;
myValues[2] = CFNumberCreate(NULL, kCFNumberFloatType, &compression);
myOptions = CFDictionaryCreate( NULL, (const void **)myKeys, (const void **)myValues, 3,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
// Release the CFNumber and CFDictionary objects when you no longer need them.
我不明白为什么他们没有继续下去,我猜接下来的步骤会是:
- 创建一个
CFImageDestinationRef
- 将你的图片复制到它里面
- 在上面设置所需的元数据
从文档中并不清楚如何执行第一步。因此,我查看了CGImageDestination
参考文献,看起来可以这样做(未经测试):
NSMutableData* mutableData = [[NSMutableData alloc] initWithCapacity:[pngData length]];
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData((__bridge_transfer CFMutableDataRef)mutableData, <# Some UTI Type #>, 1, NULL);
CGImageDestinationAddImage(imageDestination, image, yourProperties);
CGImageDestinationFinalize(imageDestination);
按照Apple文档中所示的方式创建属性字典,然后创建图像目标并将所有内容(包括属性)写入其中。完成后,您可以访问NSMutableData
对象并读取图像数据。