Created
September 13, 2012 16:32
-
-
Save orta/3715588 to your computer and use it in GitHub Desktop.
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+ImmediateLoading.h | |
// Code taken from https://gist.github.com/259357 | |
// | |
#import <Foundation/Foundation.h> | |
@interface UIImage (UIImage_ImmediateLoading) | |
- (UIImage*) initImmediateLoadWithContentsOfFile:(NSString*)path; | |
+ (UIImage*)imageImmediateLoadWithContentsOfFile:(NSString*)path; | |
@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+ImmediateLoading.h | |
// Code taken from https://gist.github.com/259357 | |
// | |
#import "UIImage+ImmediateLoading.h" | |
@implementation UIImage (UIImage_ImmediateLoading) | |
+ (UIImage*)imageImmediateLoadWithContentsOfFile:(NSString*)path { | |
return [[UIImage alloc] initImmediateLoadWithContentsOfFile: path] ; | |
} | |
- (UIImage*) initImmediateLoadWithContentsOfFile:(NSString*)path { | |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path]; | |
if(!image) return nil; | |
CGImageRef imageRef = [image CGImage]; | |
CGColorSpaceRef gray = CGColorSpaceCreateDeviceGray(); | |
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef); | |
//Stack Overflow and this original gist tell us kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little | |
// is required for the work to stay off the main thread but we can't source this | |
//Considering you can't create a bitmap context for a grayscale with those flags, | |
//we'll take out chances this way | |
CGBitmapInfo info; | |
if (colorSpace == gray) { | |
info = kCGImageAlphaNone; | |
} else { | |
info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little; | |
} | |
CGColorSpaceRelease(gray); | |
CGRect rect = CGRectMake(0.f, 0.f, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); | |
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, | |
rect.size.width, | |
rect.size.height, | |
CGImageGetBitsPerComponent(imageRef), | |
CGImageGetBytesPerRow(imageRef), | |
colorSpace, | |
info | |
); | |
CGContextDrawImage(bitmapContext, rect, imageRef); | |
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(bitmapContext); | |
UIImage* decompressedImage = [[UIImage alloc] initWithCGImage: decompressedImageRef]; | |
CGImageRelease(decompressedImageRef); | |
CGContextRelease(bitmapContext); | |
return decompressedImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment