How can I load images into the iphone system cache

2019-08-20 03:20发布

问题:

I have a tableView with some large images in it. I'm struggling to improve the very jerky scrolling performance. If I use ImageNamed to load the images, scrolling is jerky at first, but after the images are viewed, scrolling is smooth. I know ImageNamed adds the images into the system cache, so my question is: is it possible to pre-load the images into the system cache before they are viewed?

I've tried by adding the following code to my viewDidLoad method:

for (int i = 0; i < appDelegate.detailSectionsDelegateDict.count; i++) {
         NSString *imageString = [NSString stringWithFormat:@"%@",[[appDelegate.detailSectionsDelegateDict objectAtIndex:i] objectForKey:@"MainTrackImage"]];
        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
        UIImage* theImage;
        theImage = [UIImage imageNamed:imageString];
         [imageCacheArray insertObject:theImage atIndex:i];
}

I then draw the correct image from the imageCacheArray in my CellForRowAtIndexPath method. But the result is still jerky scrolling.

Thanks!

回答1:

Getting a table view with images (especially large ones) to scroll smoothly is not as trivial as you might think. Loading up a bunch of images with [UIImage imageNamed:] will very quickly cause springboard to kill your app as it starts to exceed memory capacity. Take a look at the Core Animation session videos from this year's WWDC, specifically look at session 425, "Core Animation in Practice, Part 2" They cover this exact topic and it's very well done. You can also get the relevant source code if you sign in with your developer account.