Graphics Reference
In-Depth Information
In some cases, it simply isn't practical to load everything in advance. Take something like
an image carousel that may potentially contain thousands of images: The user will expect to
be able to flick through the images quickly without any slowdown, but it would be
impossible to preload all of the images; it would take too long and consume too much
memory.
Images may also sometimes need to be retrieved from a remote network connection, which
can take considerably more time than loading from the flash drive and may even fail
altogether due to connection problems (after several seconds of trying). You cannot do
network loading on the main thread and expect the user to wait for it with a frozen screen.
You need to use a background thread.
Threaded Loading
In our contacts list example in Chapter 12, “Tuning for Speed,” the images were small
enough to load in real time on the main thread as we scrolled. But for large images, this
doesn't work well because the loading takes too long and causes scrolling to stutter.
Scrolling animations are updated on the main run loop, and are therefore more vulnerable to
CPU-related performance issues than CAAnimation , which is run in the render server
process.
Listing 14.1 shows the code for a basic image carousel, implemented using
UICollectionView . The image loading is performed synchronously on the main thread in
the -collectionView:cellForItemAtIndexPath: method (see Figure 14.1 for an
example).
Listing 14.1 Implementing an Image Carousel Using UICollectionView
#import "ViewController.h"
@interface ViewController () < UICollectionViewDataSource >
@property ( nonatomic , copy ) NSArray *imagePaths;
@property ( nonatomic , weak ) IBOutlet UICollectionView *collectionView;
@end
@implementation ViewController
- ( void )viewDidLoad
{
//set up data
self . imagePaths =
[[ NSBundle mainBundle ] pathsForResourcesOfType : @"png"
inDirectory : @"Vacation Photos" ];
Search WWH ::




Custom Search