Graphics Reference
In-Depth Information
//add it to our view
[ self . layerView . layer addSublayer :blueLayer];
}
@end
Stretchable Images
Another way to create a rounded rectangle is by using a circular contents image combined
with the contentsCenter property mentioned in Chapter 2, “The Backing Image,” to
create a stretchable image (see Listing 15.2). In theory, this should be slightly faster to
render than using a CAShapeLayer because drawing a stretchable image only requires 18
triangles (a stretchable image is rendered using nine rectangles arranged in a 3x3 grid),
whereas many more are needed to render a smooth curve. In practice, the difference is
unlikely to be significant.
Listing 15.2 Drawing a Rounded Rectangle Using a Stretchable Image
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
//create layer
CALayer *blueLayer = [ CALayer layer ];
blueLayer. frame = CGRectMake ( 50 , 50 , 100 , 100 );
blueLayer. contentsCenter = CGRectMake ( 0.5 , 0.5 , 0.0 , 0.0 );
blueLayer. contentsScale = [ UIScreen mainScreen ]. scale ;
blueLayer. contents =
( __bridge id )[ UIImage imageNamed : @"Circle.png" ]. CGImage ;
//add it to our view
[ self . layerView . layer addSublayer :blueLayer];
}
@end
The advantage of using stretchable images over the other techniques is that they can be
used to draw an arbitrary border effect at no extra cost to performance. So, for example, a
stretchable image could also be used to efficiently create a rectangular drop shadow effect.
Search WWH ::




Custom Search