Skip to content

Instantly share code, notes, and snippets.

@jamztang
Created October 2, 2011 05:40
Show Gist options
  • Save jamztang/1257111 to your computer and use it in GitHub Desktop.
Save jamztang/1257111 to your computer and use it in GitHub Desktop.
Force UIImage to decompress
//
// UIImage+JTImageDecode.h
//
// Created by james on 9/28/11.
// http://ioscodesnippet.tumblr.com
//
@interface UIImage (JTImageDecode)
+ (UIImage *)decodedImageWithImage:(UIImage *)image;
@end
//
// 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