问题
I'm looking for a way to optimize my images by converting its color from 32bit to 16bit rather than just solely resize it. So this is what I'm doing:
- (UIImage *)optimizeImage:(UIImage *)image
{
float newWidth = image.size.width;
float newHeight = image.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, newWidth, newHeight, 5, newWidth * 4,
colorSpace, kCGImageAlphaNone | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGInterpolationQuality quality = kCGInterpolationHigh;
CGContextSetInterpolationQuality(context, quality);
CGImageRef srcImage = CGImageRetain(image.CGImage);
CGContextDrawImage(context, CGRectMake(0, 0, newWidth, newHeight),
srcImage);
CGImageRelease(srcImage);
CGImageRef dst = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *result = [UIImage imageWithCGImage:dst];
CGImageRelease(dst);
UIGraphicsEndImageContext();
return result;
}
The issue with this piece of code is that when I run it, I get this very error:
CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNone; 10392 bytes/row.
So my question is: what is the supported combination for CGBitmapContextCreate
? What should I select for the bitmapInfo
parameter in this situation? Please suggest.
回答1:
So I've found my answer in Mac Developer Library. There's a table for the supported pixel formats and this is what I'm looking for:
RGB - 16 bpp, 5 bpc, kCGImageAlphaNoneSkipFirst
So I've changed my bitmapInfo
and the context's created just fine. Hopefully this is useful for someone.
来源:https://stackoverflow.com/questions/28801970/convert-an-image-to-a-16bit-color-image