Graphics Reference
In-Depth Information
performance indeed, as Core Animation is forced to process the image using the CPU
instead of the much faster GPU. (See Chapter 12, “Tuning for Speed,” and Chapter 13,
“Efficient Drawing,” for a more detailed explanation of software versus hardware drawing.)
CATiledLayer offers a solution for loading large images that solves the performance
problems with large images by splitting the image up into multiple small tiles and loading
them individually as needed. Let's test this out with an example.
Tile Cutting
We'll start with a fairly large image—in this case, a 2048×2048 image of a snowman. To
get any benefit from CATiledLayer , we'll need to slice this into several smaller images.
You can do this programmatically, but if you load the entire image and then cut it up at
runtime, you will lose most of the loading performance advantage that CATiledLayer is
designed to provide. Ideally, you want to do this as a preprocessing step instead.
Listing 6.11 shows the code for a simple Mac OS command-line app that can cut an image
into tiles and save them as individual files for use with a CATiledLayer .
Listing 6.11 A Terminal App for Slicing an Image into Tiles
#import <AppKit/AppKit.h>
int main( int argc, const char * argv[])
{
@autoreleasepool
{
//handle incorrect arguments
if (argc < 2 )
{
NSLog ( @"TileCutter arguments: inputfile" );
return 0 ;
}
//input file
NSString *inputFile = [ NSString stringWithCString :argv[ 1 ]
encoding : NSUTF8StringEncoding ];
//tile size
CGFloat tileSize = 256 ;
//output path
NSString *outputPath = [inputFile stringByDeletingPathExtension ];
//load image
NSImage *image = [[ NSImage alloc ] initWithContentsOfFile :inputFile];
Search WWH ::




Custom Search