Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to improve speed of CGContextDrawPath / drawInContext:
    primarykey
    data
    text
    <p>I have the following code below, It's basically one slice of a <strong>PieChart</strong> with many of these slices. Each slice is drawn in it's own <code>CALayer</code> and is added to a custom views layer using <code>addSublayer:</code>.</p> <p>The problem is that I'm updating the pie chart dynamically as the user drags their finger (They can edit the pie chart values by dragging). It works well, but there is very noticeable lag when re-drawing these pie 'slices'. I've look at the iOS Profiling tool, and it shows that >50% of the time is in <code>CGContextDrawPath()</code> as it has to redraw the pie slice each time the user moves a certain number of degrees. </p> <p>My question is, What can I do to improve the speed of this code? Is there something I'm missing?</p> <p>Also as a side note, this code is running well on an iPad 2 (acceptable levels of fps) but on iPad 3 it runs dreadful, from my estimations it is 2x slower. Can anyone explain this? Is it just the retina display?</p> <pre><code>-(void)drawInContext:(CGContextRef)ctx { // Create the path CGRect insetBounds = CGRectInset(self.bounds, self.strokeWidth, self.strokeWidth); CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2); CGFloat radius = MIN(insetBounds.size.width/2, insetBounds.size.height/2); CGContextBeginPath(ctx); CGContextMoveToPoint(ctx, center.x, center.y); CGPoint p1 = CGPointMake(center.x + radius * cosf(self.startAngle), center.y + radius * sinf(self.startAngle)); CGContextAddLineToPoint(ctx, p1.x, p1.y); int clockwise = self.startAngle &gt; self.endAngle; CGContextAddArc(ctx, center.x, center.y, radius, self.startAngle, self.endAngle, clockwise); CGContextClosePath(ctx); // Color it CGContextSetFillColorWithColor(ctx, self.fillColor.CGColor); CGContextSetStrokeColorWithColor(ctx, self.strokeColor.CGColor); CGContextSetLineWidth(ctx, self.strokeWidth); CGContextDrawPath(ctx, kCGPathFillStroke); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload