Merge pull request #1340 from lagapollo/lagapollo-patch-1
Memory usage upgrade
This commit is contained in:
commit
53ef2dc3c3
|
@ -13,49 +13,57 @@
|
||||||
@implementation UIImage (ForceDecode)
|
@implementation UIImage (ForceDecode)
|
||||||
|
|
||||||
+ (UIImage *)decodedImageWithImage:(UIImage *)image {
|
+ (UIImage *)decodedImageWithImage:(UIImage *)image {
|
||||||
// do not decode animated images
|
// while downloading huge amount of images
|
||||||
if (image.images) { return image; }
|
// autorelease the bitmap context
|
||||||
|
// and all vars to help system to free memory
|
||||||
CGImageRef imageRef = image.CGImage;
|
// when there are memory warning.
|
||||||
|
// on iOS7, do not forget to call
|
||||||
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
|
// [[SDImageCache sharedImageCache] clearMemory];
|
||||||
BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
|
@autoreleasepool{
|
||||||
alpha == kCGImageAlphaLast ||
|
// do not decode animated images
|
||||||
alpha == kCGImageAlphaPremultipliedFirst ||
|
if (image.images) { return image; }
|
||||||
alpha == kCGImageAlphaPremultipliedLast);
|
|
||||||
|
|
||||||
if (anyAlpha) { return image; }
|
|
||||||
|
|
||||||
size_t width = CGImageGetWidth(imageRef);
|
|
||||||
size_t height = CGImageGetHeight(imageRef);
|
|
||||||
|
|
||||||
// current
|
|
||||||
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
|
|
||||||
CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
|
|
||||||
|
|
||||||
bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelIndexed);
|
CGImageRef imageRef = image.CGImage;
|
||||||
if (unsupportedColorSpace)
|
|
||||||
colorspaceRef = CGColorSpaceCreateDeviceRGB();
|
|
||||||
|
|
||||||
CGContextRef context = CGBitmapContextCreate(NULL, width,
|
|
||||||
height,
|
|
||||||
CGImageGetBitsPerComponent(imageRef),
|
|
||||||
0,
|
|
||||||
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];
|
|
||||||
|
|
||||||
if (unsupportedColorSpace)
|
|
||||||
CGColorSpaceRelease(colorspaceRef);
|
|
||||||
|
|
||||||
CGContextRelease(context);
|
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
|
||||||
CGImageRelease(imageRefWithAlpha);
|
BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
|
||||||
|
alpha == kCGImageAlphaLast ||
|
||||||
|
alpha == kCGImageAlphaPremultipliedFirst ||
|
||||||
|
alpha == kCGImageAlphaPremultipliedLast);
|
||||||
|
|
||||||
return imageWithAlpha;
|
if (anyAlpha) { return image; }
|
||||||
|
|
||||||
|
size_t width = CGImageGetWidth(imageRef);
|
||||||
|
size_t height = CGImageGetHeight(imageRef);
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
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];
|
||||||
|
|
||||||
|
if (unsupportedColorSpace)
|
||||||
|
CGColorSpaceRelease(colorspaceRef);
|
||||||
|
|
||||||
|
CGContextRelease(context);
|
||||||
|
CGImageRelease(imageRefWithAlpha);
|
||||||
|
|
||||||
|
return imageWithAlpha;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Reference in New Issue