Created
October 2, 2011 05:40
-
-
Save jamztang/1257111 to your computer and use it in GitHub Desktop.
Force UIImage to decompress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImage+JTImageDecode.h | |
// | |
// Created by james on 9/28/11. | |
// http://ioscodesnippet.tumblr.com | |
// | |
@interface UIImage (JTImageDecode) | |
+ (UIImage *)decodedImageWithImage:(UIImage *)image; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImage+JTImageDecode.m | |
// | |
// Created by james on 9/28/11. | |
// http://ioscodesnippet.tumblr.com | |
// | |
@implementation UIImage (JTImageDecode) | |
+ (UIImage *)decodedImageWithImage:(UIImage *)image { | |
CGImageRef imageRef = image.CGImage; | |
// System only supports RGB, set explicitly and prevent context error | |
// if the downloaded image is not the supported format | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef context = CGBitmapContextCreate(NULL, | |
CGImageGetWidth(imageRef), | |
CGImageGetHeight(imageRef), | |
8, | |
// width * 4 will be enough because are in ARGB format, don't read from the image | |
CGImageGetWidth(imageRef) * 4, | |
colorSpace, | |
// kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little | |
// makes system don't need to do extra conversion when displayed. | |
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); | |
CGColorSpaceRelease(colorSpace); | |
if ( ! context) { | |
return nil; | |
} | |
CGRect rect = (CGRect){CGPointZero, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)}; | |
CGContextDrawImage(context, rect, imageRef); | |
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context); | |
CGContextRelease(context); | |
UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef]; | |
CGImageRelease(decompressedImageRef); | |
return [decompressedImage autorelease]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment