Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
@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
@steipete

This comment has been minimized.

Show comment Hide comment
@steipete

steipete Mar 28, 2012

Careful, this doesn't set the best byte ordering. If this is for the screen, you should use CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); instead.

Careful, this doesn't set the best byte ordering. If this is for the screen, you should use CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); instead.

@bagelturf

This comment has been minimized.

Show comment Hide comment
@bagelturf

bagelturf Mar 29, 2012

The comment by steipete above is incorrect. The code as written handles this because it is set by the UIImage. If you try the suggested optimization your code will break as soon as you try to render images with monochrome color spaces.

The comment by steipete above is incorrect. The code as written handles this because it is set by the UIImage. If you try the suggested optimization your code will break as soon as you try to render images with monochrome color spaces.

@steipete

This comment has been minimized.

Show comment Hide comment
@steipete

steipete Mar 29, 2012

True that, it's a delicate change - but then again, one also can manually set the bits per component and byts per row to not break things.

True that, it's a delicate change - but then again, one also can manually set the bits per component and byts per row to not break things.

@bdudney

This comment has been minimized.

Show comment Hide comment
@bdudney

bdudney Mar 29, 2012

Hi Steve, I'm pretty sure that the imageRef will also have an internal pointer to the decompressed bits. You could just create a new context with the UIKit functions, draw image then discard the context. The UIImage image would then have an indirect pointer to the decompressed image.

bdudney commented Mar 29, 2012

Hi Steve, I'm pretty sure that the imageRef will also have an internal pointer to the decompressed bits. You could just create a new context with the UIKit functions, draw image then discard the context. The UIImage image would then have an indirect pointer to the decompressed image.

@dannybabiy

This comment has been minimized.

Show comment Hide comment
@dannybabiy

dannybabiy Apr 20, 2012

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?

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment