Skip to content

Commit

Permalink
Fix unsupported colorspace issue.
Browse files Browse the repository at this point in the history
Without this fix, this url: https://abs.twimg.com/sticky/default_profile_images/default_profile_3_normal.png isn't correctly decoded and the method returns a nil image.. perhaps there should be a failsafe that checks the return value and returns the input image instead if the return value is nil.
  • Loading branch information
izackp committed Aug 21, 2015
1 parent 49f6e53 commit d00d368
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions SDWebImage/SDWebImageDecoder.m
Expand Up @@ -29,25 +29,29 @@ + (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));
CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);

bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelIndexed);
if (unsupportedColorSpace)
colorspaceRef = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(NULL, width,
height,
CGImageGetBitsPerComponent(imageRef),
0,
(imageColorSpaceModel == 0 || imageColorSpaceModel == -1) ? RGBcolorSpace : CGImageGetColorSpace(imageRef),
colorspaceRef,
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);
if (unsupportedColorSpace)
CGColorSpaceRelease(colorspaceRef);

CGContextRelease(context);
CGImageRelease(imageRefWithAlpha);

Expand Down

0 comments on commit d00d368

Please sign in to comment.