Creating the CGBitmapContext with the right bytes per pixel and bitmap info depending on the original image.

This removes an error that was making CGBitmapContext return NULL with some images:
<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 400 bytes/row.
This commit is contained in:
Javier Soto 2012-10-12 13:43:33 -07:00
parent 6818520744
commit 487db1b385
1 changed files with 9 additions and 4 deletions

View File

@ -99,16 +99,21 @@ static SDWebImageDecoder *sharedInstance;
CGImageRef imageRef = image.CGImage;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
BOOL imageHasAlphaInfo = (alphaInfo != kCGImageAlphaNone);
int bytesPerPixel = imageHasAlphaInfo ? 4 : 3;
CGBitmapInfo bitmapInfo = imageHasAlphaInfo ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNone;
CGContextRef context = CGBitmapContextCreate(NULL,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
8,
// Just always return width * 4 will be enough
CGImageGetWidth(imageRef) * 4,
// Just always return width * bytesPerPixel will be enough
CGImageGetWidth(imageRef) * bytesPerPixel,
// System only supports RGB, set explicitly
colorSpace,
// Makes system don't need to do extra conversion when displayed.
alphaInfo | kCGBitmapByteOrder32Little);
bitmapInfo);
CGColorSpaceRelease(colorSpace);
if (!context) return nil;