Skip to content

Instantly share code, notes, and snippets.

@zoul
Created September 14, 2010 07:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoul/578658 to your computer and use it in GitHub Desktop.
Save zoul/578658 to your computer and use it in GitHub Desktop.
Forces UIImage to load and decode its data
@implementation UIImage (ForceLoading)
- (void) forceLoad
{
const CGImageRef cgImage = [self CGImage];
const int width = CGImageGetWidth(cgImage);
const int height = CGImageGetHeight(cgImage);
const CGColorSpaceRef colorspace = CGImageGetColorSpace(cgImage);
const CGContextRef context = CGBitmapContextCreate(
NULL, /* Where to store the data. NULL = don’t care */
width, height, /* width & height */
8, width * 4, /* bits per component, bytes per row */
colorspace, kCGImageAlphaNoneSkipFirst);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
CGContextRelease(context);
}
@end
@xaphod
Copy link

xaphod commented Apr 22, 2017

didn't work for me today on iOS 10: if I have a UIImage, call this, retain the image somewhere, then immediately it from disk, i cannot use it properly anymore (it appears garbled). If I don't delete it, it's fine.

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