将图像转换为16位彩色图像 [英] Convert an image to a 16bit color image
问题描述
我正在寻找一种方法来优化图像,方法是将其颜色从32位转换为16位,而不是仅仅调整其大小.所以这就是我正在做的:
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:不支持的参数组合:5个整数 位/分量; 16位/像素; 3分量色彩空间; kCGImageAlphaNone; 10392字节/行.
CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNone; 10392 bytes/row.
所以我的问题是:CGBitmapContextCreate
支持的组合是什么?在这种情况下,我应该为bitmapInfo
参数选择什么?请提出建议.
So my question is: what is the supported combination for CGBitmapContextCreate
? What should I select for the bitmapInfo
parameter in this situation? Please suggest.
推荐答案
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
RGB - 16 bpp, 5 bpc, kCGImageAlphaNoneSkipFirst
因此,我已经更改了bitmapInfo
,并且上下文的创建就很好了.希望这对某人有用.
So I've changed my bitmapInfo
and the context's created just fine. Hopefully this is useful for someone.
这篇关于将图像转换为16位彩色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!