Graphics Reference
In-Depth Information
memory equal to the width × height of the layer (in pixels, not points) × 4 bytes. For a full-
screen layer on a Retina iPad, that's 2048 × 1536 × 4 bytes, which amounts to a whole
12MB that must not only be stored in RAM, but must be wiped and repopulated every time
the layer is redrawn.
Because software drawing is so expensive, you should avoid redrawing your view unless
absolutely necessary. The secret to improving drawing performance is generally to try to do
as little drawing as possible.
Vector Graphics
A common reason to use Core Graphics drawing is for vector graphics that cannot easily be
created using images or layer effects. Vector drawing might involve the following:
Arbitrary polygonal shapes (anything other than a rectangle)
Diagonal or curved lines
Text
Gradients
Let's try an example. Listing 13.1 shows the code for a basic line-drawing app. The app
converts user touches into points on a UIBezierPath , which is then drawn into the view.
We've included all the drawing logic in a UIView subclass called DrawingView in this case
(the view controller is empty), but you could implement the touch handling in the view
controller instead if you prefer. Figure 13.1 shows a drawing created by using this app.
Listing 13.1 Implementing a Simple Drawing App Using Core Graphics
#import "DrawingView.h"
@interface DrawingView ()
@property ( nonatomic , strong ) UIBezierPath *path;
@end
@implementation DrawingView
- ( void )awakeFromNib
{
//create a mutable path
self . path = [[ UIBezierPath alloc ] init ];
self . path . lineJoinStyle = kCGLineJoinRound ;
self . path . lineCapStyle = kCGLineCapRound ;
 
Search WWH ::




Custom Search