Merge pull request #195 from JaviSoto/bitmap-info-error

Creating the CGBitmapContext with the right bytes per pixel and bitmap info depending on the original image.
This commit is contained in:
Olivier Poitrey 2012-10-12 15:22:03 -07:00
commit 0102bdf7b7
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;