2010-12-17 24 views
0

我正在移交給此方法,並使用得到CGImageRef其添加到CGImageDestinationRef是設置爲最後確定爲kUTTypeJPEG轉換爲JPEG時應該如何考慮CGImageAlphaInfo?

- (CGImageRef)scaleImage:(CGImageRef)fullImageRef originalSize:(CGSize)originalSize scale:(CGFloat)scale; 
    { 
     CGSize imageSize = originalSize; 

     CGRect scaledRect = CGRectMake(0.0, 0.0, imageSize.width * scale, imageSize.height * scale); 
     CGColorSpaceRef colorSpace = CGImageGetColorSpace(fullImageRef); 
     CGContextRef context = CGBitmapContextCreate(NULL, scaledRect.size.width, scaledRect.size.height, 8, 0, colorSpace, CGImageGetAlphaInfo(fullImageRef)); 
     CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
     CGImageRef subImage = CGImageCreateWithImageInRect(fullImageRef, scaledRect); 
     CGContextDrawImage(context, scaledRect, subImage); // offscreen 
     CGImageRef scaledImage = CGBitmapContextCreateImage(context); 
     CGImageRelease(subImage); 
     subImage = NULL; 
     subImage = scaledImage; 
     CGImageRelease(scaledImage); 
     CGContextFlush(context); 
     CGContextRelease(context); 
     return subImage; 
    } 

我需要確保所得到的JPEG是最好的顏色匹配原始。

我不清楚CGImageAlphaInfo常量對我的目的的實際作用。

我已經讀過Quartz文檔,也有優秀的Quartz編程書籍(由Gelphman和Laden編寫),但我仍然不確定我的方法。

我已經在jpeg的屬性字典中將屬性kCGImageDestinationLossyCompressionQuality設置爲1.0,並捕獲其他屬性。

我應該做更多的事情來保持原有的顏色完整性?

這是macos,並使用gc。

回答

1

這並不重要,因爲JPEG不支持alpha(至少不是沒有external maskingan extension),所以您可以期望CGImageDestination將在導出階段丟棄Alpha通道。

我會先嚐試原始圖像的alpha信息,如果我不能以這種方式創建位圖上下文,我會使用kCGImageAlphaNoneSkipFirstkCGImageAlphaNoneSkipLast。對於其他目標文件格式,當然,我會使用alpha-with-preultiplied-colors像素格式之一。

This page有CGBitmapContext支持的組合的完整列表。

+0

謝謝,彼得。這正是我需要的信息。 – lulu 2010-12-19 15:22:13

相關問題