user1643881
user1643881

Reputation: 61

CGBitmapContextCreate unsupported parameter combination error

I'm reading an image file and re-displaying it without making any change as shown in the code below, but I get the this error:

: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaPremultipliedLast; 3792 bytes/row.

The code is:

CGImageRef sourceImage = theImage.image.CGImage;

CFDataRef theData;
theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));

UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);

CGContextRef context;
context = CGBitmapContextCreate(pixelData,
                                 CGImageGetWidth(sourceImage),
                                 CGImageGetHeight(sourceImage),
                                 8,
                                 CGImageGetBytesPerRow(sourceImage),
                                 CGImageGetColorSpace(sourceImage),
                                 kCGImageAlphaPremultipliedLast);

CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newCGImage];

CGContextRelease(context);
CFRelease(theData);
CGImageRelease(newCGImage);

theImage.image = newImage;

This code used to work fine until I upgraded to Xcode 4.4 recently.

Following a suggestion in iPhone CGContextRef CGBitmapContextCreate unsupported parameter combination, I replaced "CGImageGetColorSpace(sourceImage)," by "CGColorSpaceCreateDeviceRGB(),", but got this error:

: CGBitmapContextCreate: invalid data bytes/row: should be at least 15168 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedLast.

Then, when I replaced "CGImageGetBytesPerRow(sourceImage)," by "CGImageGetBytesPerRow(sourceImage)*4," the errors were gone but for copies of the 4X reduced image were displayed side-by-side.

Has anybody encountered this problem? Is it related to the Xcode 4.4 upgrade?

Thanks! Shai

Upvotes: 2

Views: 5222

Answers (1)

user1643881
user1643881

Reputation: 61

Solved by using kCGImageAlphaNone instead of kCGImageAlphaPremultipliedLast. I still dont understand why/how my color image is turned into grayscale, and why something that used to work changed its behavior, but at least I get some display.

Upvotes: 4

Related Questions