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