2014-07-17 58 views
1

我在CMYK顏色空間中有CGImage(即CGColorSpaceGetModel在通過CGImageGetColorSpace查詢時返回kCGColorSpaceModelCMYK)。CGImageDestination將CMYK顏色空間保存爲RGB

但是,當我嘗試通過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(獅子),如果這是一個因素,

回答

2

好吧,我咬了子彈和使用的事件讓蘋果的技術幫助。這裏是他們的答覆:

答案有兩個部分:

1)如果目標文件格式支持色彩空間的ImageIO將保留CMYK和Lab色彩空間。目前,支持CMYK的圖像格式爲TIFF,PSD,JPEG和JPEG 2000.如果目標文件格式不支持當前色彩空間,ImageIO將轉換爲標準RGB配置文件。出現這種情況,例如,如果你打算保存CMYK圖像PNG或BMP格式的文件

2)的ImageIO不支持節能CMYK或Lab爲JPEG 2000,儘管規範規定的文件格式支持CMYK和實驗室。 Apple Engineering承認,目前這是ImageIO的缺點。這可能會在未來發生變化。

所以,以上的代碼是正確。您將不得不自己過濾文件目標類型(UTI),或警告用戶即將進行轉換。未來,當Apple向JPEG 2000添加CMYK和Lab支持時,您必須更改濾鏡功能,但代碼可以保持不變。