Graphics Reference
In-Depth Information
CGContextGetClipBoundingBox() function to get this from the context itself. This is
simpler when using -drawRect: because the CGRect is passed directly as a parameter.
You should try to limit your drawing to only the parts of the image that overlap this
rectangle. Anything you draw outside of the dirty CGRect will be clipped automatically, but
the CPU time spent on evaluating and discarding those redundant drawing commands will
be wasted.
By clipping your own drawing rather than relying on Core Graphics to do it for you, you
may be able to avoid unnecessary processing. That said, if your clipping logic is complex,
you might be better off letting Core Graphics handle it; only clip your own drawing if you
can do so efficiently.
Listing 13.4 shows an updated version of our -addBrushStrokeAtPoint: method that only
redraws the rectangle around the current brush stroke. In addition to only refreshing the part
of the view around the last brush stroke, we also use CGRectIntersectsRect() to avoid
redrawing any old brush strokes that don't overlap the updated region. This significantly
improves the drawing performance (see Figure 13.4).
Listing 13.4 Using setNeedsDisplayInRect: to Reduce Unnecessary Drawing
- ( void )addBrushStrokeAtPoint:( CGPoint )point
{
//add brush stroke to array
[ self . strokes addObject :[ NSValue valueWithCGPoint :point]];
//set dirty rect
[ self setNeedsDisplayInRect :[ self brushRectForPoint :point]];
}
- ( CGRect )brushRectForPoint:( CGPoint )point
{
return CGRectMake (point. x - BRUSH_SIZE / 2 ,
point. y - BRUSH_SIZE / 2 ,
BRUSH_SIZE , BRUSH_SIZE );
}
- ( void )drawRect:( CGRect )rect
{
//redraw strokes
for ( NSValue *value in self . strokes )
{
//get point
CGPoint point = [value CGPointValue ];
//get brush rect
CGRect brushRect = [ self brushRectForPoint :point];
Search WWH ::




Custom Search