@@ -29,25 +29,29 @@ + (UIImage *)decodedImageWithImage:(UIImage *)image {
29
29
size_t width = CGImageGetWidth (imageRef);
30
30
size_t height = CGImageGetHeight (imageRef);
31
31
32
- // default RGB
33
- CGColorSpaceRef RGBcolorSpace = CGColorSpaceCreateDeviceRGB ();
34
-
35
32
// current
36
33
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel (CGImageGetColorSpace (imageRef));
34
+ CGColorSpaceRef colorspaceRef = CGImageGetColorSpace (imageRef);
35
+
36
+ bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelIndexed );
37
+ if (unsupportedColorSpace)
38
+ colorspaceRef = CGColorSpaceCreateDeviceRGB ();
37
39
38
40
CGContextRef context = CGBitmapContextCreate (NULL , width,
39
41
height,
40
42
CGImageGetBitsPerComponent (imageRef),
41
43
0 ,
42
- (imageColorSpaceModel == 0 || imageColorSpaceModel == - 1 ) ? RGBcolorSpace : CGImageGetColorSpace (imageRef) ,
44
+ colorspaceRef ,
43
45
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst );
44
46
45
47
// Draw the image into the context and retrieve the new image, which will now have an alpha layer
46
48
CGContextDrawImage (context, CGRectMake (0 , 0 , width, height), imageRef);
47
49
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage (context);
48
50
UIImage *imageWithAlpha = [UIImage imageWithCGImage: imageRefWithAlpha];
49
51
50
- CGColorSpaceRelease (RGBcolorSpace);
52
+ if (unsupportedColorSpace)
53
+ CGColorSpaceRelease (colorspaceRef);
54
+
51
55
CGContextRelease (context);
52
56
CGImageRelease (imageRefWithAlpha);
53
57
0 commit comments