Fix unsupported colorspace issue.

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.
This commit is contained in:
Isaac Paul 2015-08-21 10:59:07 -04:00
parent 49f6e532b5
commit d00d368cdb
1 changed files with 9 additions and 5 deletions

View File

@ -29,17 +29,19 @@
size_t width = CGImageGetWidth(imageRef); size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef); size_t height = CGImageGetHeight(imageRef);
// default RGB
CGColorSpaceRef RGBcolorSpace = CGColorSpaceCreateDeviceRGB();
// current // current
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); 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, CGContextRef context = CGBitmapContextCreate(NULL, width,
height, height,
CGImageGetBitsPerComponent(imageRef), CGImageGetBitsPerComponent(imageRef),
0, 0,
(imageColorSpaceModel == 0 || imageColorSpaceModel == -1) ? RGBcolorSpace : CGImageGetColorSpace(imageRef), colorspaceRef,
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
// Draw the image into the context and retrieve the new image, which will now have an alpha layer // Draw the image into the context and retrieve the new image, which will now have an alpha layer
@ -47,7 +49,9 @@
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context); CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha]; UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];
CGColorSpaceRelease(RGBcolorSpace); if (unsupportedColorSpace)
CGColorSpaceRelease(colorspaceRef);
CGContextRelease(context); CGContextRelease(context);
CGImageRelease(imageRefWithAlpha); CGImageRelease(imageRefWithAlpha);