Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
938 views
in Technique[技术] by (71.8m points)

ios - Convert an image to a 16bit color image

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...