Skip to content

Commit

Permalink
better handling of colorspace models
Browse files Browse the repository at this point in the history
  • Loading branch information
mythodeia committed Jul 16, 2015
1 parent 25b4c17 commit fb62c3f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions SDWebImage/SDWebImageDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,28 @@ + (UIImage *)decodedImageWithImage:(UIImage *)image {
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);

// default RGB
CGColorSpaceRef RGBcolorSpace = CGColorSpaceCreateDeviceRGB();

// current
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));

CGContextRef context = CGBitmapContextCreate(NULL, width,
height,
CGImageGetBitsPerComponent(imageRef),
0,
CGImageGetColorSpace(imageRef),
(imageColorSpaceModel == 0 || imageColorSpaceModel == -1) ? RGBcolorSpace : CGImageGetColorSpace(imageRef),
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

// Draw the image into the context and retrieve the new image, which will now have an alpha layer
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];

CGColorSpaceRelease(RGBcolorSpace);
CGContextRelease(context);
CGImageRelease(imageRefWithAlpha);

return imageWithAlpha;
}

Expand Down

0 comments on commit fb62c3f

Please sign in to comment.