Convert an image to a 16bit color image

亡梦爱人 提交于 2019-12-18 09:21:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!