Created
December 18, 2009 08:07
-
-
Save takuma104/259357 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
@interface UIImage(ImmediateLoad) | |
+ (UIImage*)imageImmediateLoadWithContentsOfFile:(NSString*)path; | |
@end | |
@implementation UIImage(ImmediateLoad) | |
+ (UIImage*)imageImmediateLoadWithContentsOfFile:(NSString*)path { | |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path]; | |
CGImageRef imageRef = [image CGImage]; | |
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), | |
CGImageGetColorSpace(imageRef), | |
CGImageGetBitmapInfo(imageRef) | |
); | |
CGContextDrawImage(bitmapContext, rect, imageRef); | |
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(bitmapContext); | |
UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef]; | |
CGImageRelease(decompressedImageRef); | |
CGContextRelease(bitmapContext); | |
[image release]; | |
return decompressedImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The snippet works on my iphone, but then in the simulator it doesn't work. I get something like : CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 1440 bytes/row.
I noticed that for the same troubled image, on the device CGImageGetBytesPerRow(imageRef) == 1920 and width*4 == 1920, but in the simulator CGImageGetBytesPerRow(imageRef) == 1440.
I then tried what steipete suggested and it works on both the device and simulator. Thoughts?