Optimize the decoder to avoid of unwanted blended layer
This commit is contained in:
parent
0da78a4ce6
commit
32787175ac
|
@ -19,14 +19,16 @@
|
||||||
// when there are memory warning.
|
// when there are memory warning.
|
||||||
// on iOS7, do not forget to call
|
// on iOS7, do not forget to call
|
||||||
// [[SDImageCache sharedImageCache] clearMemory];
|
// [[SDImageCache sharedImageCache] clearMemory];
|
||||||
|
|
||||||
if (image == nil) { // Prevent "CGBitmapContextCreateImage: invalid context 0x0" error
|
if (image == nil) { // Prevent "CGBitmapContextCreateImage: invalid context 0x0" error
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
@autoreleasepool{
|
@autoreleasepool{
|
||||||
// do not decode animated images
|
// do not decode animated images
|
||||||
if (image.images) { return image; }
|
if (image.images != nil) {
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
CGImageRef imageRef = image.CGImage;
|
CGImageRef imageRef = image.CGImage;
|
||||||
|
|
||||||
|
@ -35,43 +37,55 @@
|
||||||
alpha == kCGImageAlphaLast ||
|
alpha == kCGImageAlphaLast ||
|
||||||
alpha == kCGImageAlphaPremultipliedFirst ||
|
alpha == kCGImageAlphaPremultipliedFirst ||
|
||||||
alpha == kCGImageAlphaPremultipliedLast);
|
alpha == kCGImageAlphaPremultipliedLast);
|
||||||
|
if (anyAlpha) {
|
||||||
if (anyAlpha) { return image; }
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
// current
|
// current
|
||||||
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
|
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
|
||||||
CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
|
CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
|
||||||
|
|
||||||
bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed);
|
BOOL unsupportedColorSpace = (imageColorSpaceModel == kCGColorSpaceModelUnknown ||
|
||||||
if (unsupportedColorSpace)
|
imageColorSpaceModel == kCGColorSpaceModelMonochrome ||
|
||||||
|
imageColorSpaceModel == kCGColorSpaceModelCMYK ||
|
||||||
|
imageColorSpaceModel == kCGColorSpaceModelIndexed);
|
||||||
|
if (unsupportedColorSpace) {
|
||||||
colorspaceRef = CGColorSpaceCreateDeviceRGB();
|
colorspaceRef = CGColorSpaceCreateDeviceRGB();
|
||||||
|
}
|
||||||
|
|
||||||
size_t width = CGImageGetWidth(imageRef);
|
size_t width = CGImageGetWidth(imageRef);
|
||||||
size_t height = CGImageGetHeight(imageRef);
|
size_t height = CGImageGetHeight(imageRef);
|
||||||
NSUInteger bytesPerPixel = 4;
|
NSUInteger bytesPerPixel = 4;
|
||||||
NSUInteger bytesPerRow = bytesPerPixel * width;
|
NSUInteger bytesPerRow = bytesPerPixel * width;
|
||||||
NSUInteger bitsPerComponent = 8;
|
NSUInteger bitsPerComponent = 8;
|
||||||
|
|
||||||
|
|
||||||
|
// kCGImageAlphaNone is not supported in CGBitmapContextCreate.
|
||||||
|
// Since the original image here has no alpha info, use kCGImageAlphaNoneSkipLast
|
||||||
|
// to create bitmap graphics contexts without alpha info.
|
||||||
CGContextRef context = CGBitmapContextCreate(NULL,
|
CGContextRef context = CGBitmapContextCreate(NULL,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
bitsPerComponent,
|
bitsPerComponent,
|
||||||
bytesPerRow,
|
bytesPerRow,
|
||||||
colorspaceRef,
|
colorspaceRef,
|
||||||
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
|
kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast);
|
||||||
|
|
||||||
// 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 bitmap image without alpha
|
||||||
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
|
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
|
||||||
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
|
CGImageRef imageRefWithoutAlpha = CGBitmapContextCreateImage(context);
|
||||||
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha scale:image.scale orientation:image.imageOrientation];
|
UIImage *imageWithoutAlpha = [UIImage imageWithCGImage:imageRefWithoutAlpha
|
||||||
|
scale:image.scale
|
||||||
|
orientation:image.imageOrientation];
|
||||||
|
|
||||||
if (unsupportedColorSpace)
|
if (unsupportedColorSpace) {
|
||||||
CGColorSpaceRelease(colorspaceRef);
|
CGColorSpaceRelease(colorspaceRef);
|
||||||
|
}
|
||||||
|
|
||||||
CGContextRelease(context);
|
CGContextRelease(context);
|
||||||
CGImageRelease(imageRefWithAlpha);
|
CGImageRelease(imageRefWithoutAlpha);
|
||||||
|
|
||||||
return imageWithAlpha;
|
return imageWithoutAlpha;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue