I want to preload images sot that when I add their UIImageView to my currentView, the PNG are already in memory, loaded, inflated etc.
As you can see from this time profiling, the images are loaded when they are displayed:
In the same time, I'm already preloading these images before. Just when I load that view, I thoroughly create all images and image views for these animations using that code:
- (UIImageView*)createAnimationForReel:(NSString*)reel ofType:(NSString*)type usingFrames:(NSInteger)frames{
UIImageView *imageView = [[UIImageView alloc] init];
NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:frames];
for (int i=0;i<frames;i++){
NSString *image;
if(i<10){
image = [NSString stringWithFormat:@"%@_0%i", reel, i];
} else {
image = [NSString stringWithFormat:@"%@_%i", reel, i];
}
NSString *path = [[NSBundle mainBundle] pathForResource:image ofType:@"png" inDirectory:type];
UIImage *anImage = [[UIImage alloc] initWithContentsOfFile:path];
[imageArray addObject:anImage];
}
imageView.animationImages = [NSArray arrayWithArray:imageArray];
imageView.animationDuration = 2.0;
return imageView;
}
And when I read the documentation it says: "This method loads the image data into memory and marks it as purgeable. If the data is purged and needs to be reloaded, the image object loads that data again from the specified path." talking about initWithContentOfFile. So my image "should" be loaded.
But no.
Surely something is missing in my reflection. But what?
To force the full preloading of a UIImage, you'll need to actually draw it, or so it seems. For example using:
- (void)preload:(UIImage *)image
{
CGImageRef ref = image.CGImage;
size_t width = CGImageGetWidth(ref);
size_t height = CGImageGetHeight(ref);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * 4, space, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(space);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), ref);
CGContextRelease(context);
}
Not pretty, but it makes drawing the image about 4x faster. I tested this with an 1 MB 640 × 1136 PNG on an iPhone 4S:
Check https://gist.github.com/3923434 for the test code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With