2

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

4

1 回答 1

4

通过使用 kCGImageAlphaNone 而不是 kCGImageAlphaPremultipliedLast 解决。我仍然不明白为什么/如何将我的彩色图像变成灰度,以及为什么曾经工作的东西改变了它的行为,但至少我得到了一些显示。

于 2012-09-07T06:16:04.037 回答