将图像转换为 16 位彩色图像

huo*_*ong 3 objective-c cgbitmapcontextcreate ios

我正在寻找一种方法来优化图像,将其颜色从 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;
}
Run Code Online (Sandbox Code Playgroud)

这段代码的问题是,当我运行它时,我收到以下错误:

CGBitmapContextCreate:不支持的参数组合:5 个整数位/组件;16 位/像素;三分量色彩空间;kCGImageAlphaNone; 10392 字节/行。

所以我的问题是:支持的组合是什么CGBitmapContextCreate?这种情况下参数应该选择什么bitmapInfo?请建议。

huo*_*ong 6

所以我在Mac Developer Library中找到了答案。有一个支持的像素格式的表格,这就是我正在寻找的:

RGB - 16 bpp、5 bpc、kCGImageAlphaNoneSkipFirst

所以我改变了我的bitmapInfo并且上下文创建得很好。希望这对某人有用。