Optimize the decoder to avoid of unwanted blended layer
Showing
1 changed file
with
29 additions
and
15 deletions
@@ -19,14 +19,16 @@ | @@ -19,14 +19,16 @@ | ||
19 | // when there are memory warning. | 19 | // when there are memory warning. |
20 | // on iOS7, do not forget to call | 20 | // on iOS7, do not forget to call |
21 | // [[SDImageCache sharedImageCache] clearMemory]; | 21 | // [[SDImageCache sharedImageCache] clearMemory]; |
22 | - | 22 | + |
23 | if (image == nil) { // Prevent "CGBitmapContextCreateImage: invalid context 0x0" error | 23 | if (image == nil) { // Prevent "CGBitmapContextCreateImage: invalid context 0x0" error |
24 | return nil; | 24 | return nil; |
25 | } | 25 | } |
26 | - | 26 | + |
27 | @autoreleasepool{ | 27 | @autoreleasepool{ |
28 | // do not decode animated images | 28 | // do not decode animated images |
29 | - if (image.images) { return image; } | 29 | + if (image.images != nil) { |
30 | + return image; | ||
31 | + } | ||
30 | 32 | ||
31 | CGImageRef imageRef = image.CGImage; | 33 | CGImageRef imageRef = image.CGImage; |
32 | 34 | ||
@@ -35,43 +37,55 @@ | @@ -35,43 +37,55 @@ | ||
35 | alpha == kCGImageAlphaLast || | 37 | alpha == kCGImageAlphaLast || |
36 | alpha == kCGImageAlphaPremultipliedFirst || | 38 | alpha == kCGImageAlphaPremultipliedFirst || |
37 | alpha == kCGImageAlphaPremultipliedLast); | 39 | alpha == kCGImageAlphaPremultipliedLast); |
38 | - | ||
39 | - if (anyAlpha) { return image; } | 40 | + if (anyAlpha) { |
41 | + return image; | ||
42 | + } | ||
40 | 43 | ||
41 | // current | 44 | // current |
42 | CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); | 45 | CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); |
43 | CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef); | 46 | CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef); |
44 | 47 | ||
45 | - bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed); | ||
46 | - if (unsupportedColorSpace) | 48 | + BOOL unsupportedColorSpace = (imageColorSpaceModel == kCGColorSpaceModelUnknown || |
49 | + imageColorSpaceModel == kCGColorSpaceModelMonochrome || | ||
50 | + imageColorSpaceModel == kCGColorSpaceModelCMYK || | ||
51 | + imageColorSpaceModel == kCGColorSpaceModelIndexed); | ||
52 | + if (unsupportedColorSpace) { | ||
47 | colorspaceRef = CGColorSpaceCreateDeviceRGB(); | 53 | colorspaceRef = CGColorSpaceCreateDeviceRGB(); |
54 | + } | ||
48 | 55 | ||
49 | size_t width = CGImageGetWidth(imageRef); | 56 | size_t width = CGImageGetWidth(imageRef); |
50 | size_t height = CGImageGetHeight(imageRef); | 57 | size_t height = CGImageGetHeight(imageRef); |
51 | NSUInteger bytesPerPixel = 4; | 58 | NSUInteger bytesPerPixel = 4; |
52 | NSUInteger bytesPerRow = bytesPerPixel * width; | 59 | NSUInteger bytesPerRow = bytesPerPixel * width; |
53 | NSUInteger bitsPerComponent = 8; | 60 | NSUInteger bitsPerComponent = 8; |
54 | - | 61 | + |
62 | + | ||
63 | + // kCGImageAlphaNone is not supported in CGBitmapContextCreate. | ||
64 | + // Since the original image here has no alpha info, use kCGImageAlphaNoneSkipLast | ||
65 | + // to create bitmap graphics contexts without alpha info. | ||
55 | CGContextRef context = CGBitmapContextCreate(NULL, | 66 | CGContextRef context = CGBitmapContextCreate(NULL, |
56 | width, | 67 | width, |
57 | height, | 68 | height, |
58 | bitsPerComponent, | 69 | bitsPerComponent, |
59 | bytesPerRow, | 70 | bytesPerRow, |
60 | colorspaceRef, | 71 | colorspaceRef, |
61 | - kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); | 72 | + kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast); |
62 | 73 | ||
63 | - // Draw the image into the context and retrieve the new image, which will now have an alpha layer | 74 | + // Draw the image into the context and retrieve the new bitmap image without alpha |
64 | CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); | 75 | CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); |
65 | - CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context); | ||
66 | - UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha scale:image.scale orientation:image.imageOrientation]; | 76 | + CGImageRef imageRefWithoutAlpha = CGBitmapContextCreateImage(context); |
77 | + UIImage *imageWithoutAlpha = [UIImage imageWithCGImage:imageRefWithoutAlpha | ||
78 | + scale:image.scale | ||
79 | + orientation:image.imageOrientation]; | ||
67 | 80 | ||
68 | - if (unsupportedColorSpace) | 81 | + if (unsupportedColorSpace) { |
69 | CGColorSpaceRelease(colorspaceRef); | 82 | CGColorSpaceRelease(colorspaceRef); |
83 | + } | ||
70 | 84 | ||
71 | CGContextRelease(context); | 85 | CGContextRelease(context); |
72 | - CGImageRelease(imageRefWithAlpha); | 86 | + CGImageRelease(imageRefWithoutAlpha); |
73 | 87 | ||
74 | - return imageWithAlpha; | 88 | + return imageWithoutAlpha; |
75 | } | 89 | } |
76 | } | 90 | } |
77 | 91 |
-
Please register or login to post a comment