我有一个 CMYK 颜色空间中的 CGImage(即,当通过 CGImageGetColorSpace 查询时,CGColorSpaceGetModel 返回 kCGColorSpaceModelCMYK)。
但是,当我尝试通过 CGImageDestination 保存它时,它会转换为 RGBColorSpace。我尝试设置正确的字典值,但无济于事。我怎么知道这个?当我使用预览打开保存的图像时,“常规信息”选项卡告诉我“ColorModel:RGB”和“配置文件名称:通用 RGB 配置文件”。如果我使用 CGImageSource 打开保存的文件并读出属性,我会得到相同的结果(RGB 颜色模型、通用 RGB 配置文件)。
现在,在我失去最后一根头发(单数)之前,我的问题是:这是故意的吗? ImageIO 是否不能以 RGB 以外的任何格式保存图像?只是为了好玩,我尝试使用 L*a*b 色彩空间进行相同的操作,并得到相同的结果。
这是我的保存代码:
BOOL saveImage(CGImageRef theImage, NSURL *fileDestination, NSString *theUTI)
{
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileDestination, (__bridge CFStringRef) theUTI, 1, nil);
// now assemble the dictionary for saving
NSMutableDictionary *theDict = nil;
theDict = [[NSMutableDictionary alloc] initWithCapacity:10];
[theDict setObject:[NSNumber numberWithFloat:hdpi] forKey: (id) kCGImagePropertyDPIHeight];
[theDict setObject:[NSNumber numberWithFloat:vdpi] forKey: (id) kCGImagePropertyDPIWidth];
}
// now make sure that we have the correct color space in the dictionary
NSString *colorModel = nil;
CGColorSpaceRef theColorSpace = CGImageGetColorSpace(theImage);
CGColorSpaceModel theModel = CGColorSpaceGetModel(theColorSpace);
switch (theModel) {
case kCGColorSpaceModelRGB:
colorModel = kCGImagePropertyColorModelRGB;
break;
case kCGColorSpaceModelLab:
colorModel = kCGImagePropertyColorModelLab;
break;
case kCGColorSpaceModelCMYK:
colorModel = kCGImagePropertyColorModelCMYK;
break;
default:
colorModel = kCGImagePropertyColorModelRGB;
break;
}
[theDict setObject:colorModel forKey:(id) kCGImagePropertyColorModel];
// Add the image to the destination, characterizing the image with
// the properties dictionary.
CGImageDestinationAddImage(imageDestination, theImage, (__bridge CFDictionaryRef) theDict); //properties);
BOOL success = (CGImageDestinationFinalize(imageDestination) != 0);
CFRelease(imageDestination);
return success;
}
一定有一些明显的东西被我忽略了。我是否必须设置一些 kCFSuperSecretProperty 才能使其正常工作?或者 ImageIO 根本不保存为 CMYK?
哦,如果这是一个因素的话,我使用的是 OSX 10.7 (Lion),
最佳答案
好吧,我硬着头皮利用一次事件获得了 Apple 工程部门的帮助。这是他们的回复:
答案分为两部分:
1) 如果目标文件格式支持 CMYK 和 Lab 颜色空间,ImageIO 将保留该颜色空间。目前,支持 CMYK 的图像格式有 TIFF、PSD、JPEG 和 JPEG 2000。如果目标文件格式不支持当前颜色空间,ImageIO 将转换为标准 RGB 配置文件。例如,如果您打算将 CMYK 图像保存为 PNG 或 BMP 文件格式,就会发生这种情况
2) ImageIO 不支持将 CMYK 或 Lab 保存为 JPEG 2000,即使规范声明文件格式支持 CMYK 和 Lab。 Apple 工程部门承认这是 ImageIO 目前的一个缺点。这将来可能会改变。
所以,上面的代码是正确的。您必须自行过滤文件目标类型 (UTI) 或警告用户即将进行转换。将来,当 Apple 在 JPEG 2000 中添加 CMYK 和 Lab 支持时,您必须更改滤镜功能,但代码可以保持不变。
关于cocoa - CGImageDestination 将 CMYK 颜色空间保存为 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24810242/