[英]Convert an image to a 16bit color image
我正在寻找一种方法来优化图像,方法是将其颜色从32位转换为16位,而不仅仅是调整其大小。 所以这就是我在做什么:
- (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;
}
这段代码的问题是,当我运行它时,我得到了这个错误:
CGBitmapContextCreate:不支持的参数组合:5个整数位/组件; 16位/像素; 3分量色彩空间; kCGImageAlphaNone; 10392字节/行。
所以我的问题是: CGBitmapContextCreate
支持的组合是CGBitmapContextCreate
? 在这种情况下,应为bitmapInfo
参数选择什么? 请提出建议。
因此,我已经在Mac Developer Library中找到了答案。 有一张支持的像素格式的表格,这就是我想要的:
RGB-16 bpp,5 bpc,kCGImageAlphaNoneSkipFirst
因此,我更改了bitmapInfo
并创建了上下文。 希望这对某人有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.