Skip to content

Commit

Permalink
Merge pull request SDWebImage#1236 from mythodeia/master
Browse files Browse the repository at this point in the history
better handling of colorspace models
  • Loading branch information
bpoplauschi committed Jul 31, 2015
2 parents a45c722 + fb62c3f commit 0df58e4
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 0df58e4

Please sign in to comment.