Graphics Reference
In-Depth Information
We will use this to convert our 2048×2048 snowman image into 64 individual 256×256
tiles. (256×256 is the default tile size for CATiledLayer , although this can be changed
using the tileSize property.) Our app expects to receive the path to the input image file
as the first command-line parameter. We could hard-code this path argument in the build
scheme so that we can run it from within Xcode, but that's not very useful if we want to use
a different image in future. Instead, we'll build the app and save it somewhere sensible, and
then invoke it from the Terminal, like this:
> path/to/TileCutterApp path/to/Snowman.jpg
The app is very basic, but could easily be extended to support additional arguments such as
tile size, or to export images in formats other than JPEG. The result of running it is a
sequence of 64 new images, named as follows:
Snowman_00_00.jpg
Snowman_00_01.jpg
Snowman_00_02.jpg
Snowman_07_07.jpg
Now that we have the tile images, we need to set up an iOS application to consume them.
CATiledLayer integrates nicely with UIScrollView , so for the purposes of this
example, we'll place the CATiledLayer inside a UIScrollView . Other than setting
the layer and scrollview bounds to match our total image size, all we really need to do is
implement the -drawLayer:inContext: method, which is called by
CATiledLayer whenever it needs to load a new tile image.
Listing 6.12 shows the code, and Figure 6.12 shows the result.
Listing 6.12 A Simple Scrolling CATiledLayer Implementation
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property ( nonatomic , weak ) IBOutlet UIScrollView *scrollView;
@end
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
//add the tiled layer
CATiledLayer *tileLayer = [ CATiledLayer layer ];
Search WWH ::




Custom Search