How to increase the speed of CGContextDrawPath / drawInContext:

I have the following code below: basically this is one PieChart fragment with many of these fragments. Each slice is drawn in it by its own CALayer and is added to the user-level views using addSublayer:

The problem is that I dynamically update the pie chart when the user drags his finger (they can edit the values โ€‹โ€‹of the pie chart by dragging and dropping). It works well, but there is a very noticeable lag when re-drawing these "pieces" of cake. I look at the iOS profiling tool and it shows that> 50% of the time is in CGContextDrawPath() , because it has to redraw a piece of cake every time the user moves a certain number of degrees.

My question is: what can I do to improve the speed of this code? Is something missing?

Also, as a side note, this code works well on the iPad 2 (acceptable frame rates per second), but on the iPad 3 it is terribly scary, according to my estimates it is 2 times slower. Can someone explain this? Is this just a retina screen?

 -(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 > 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); } 
+4
source share
1 answer

Drawing graphics on an iPad 3 Retina display is slower than drawing on an iPad 2 without a retina.

I recall that Apple initially stated that the iPad 3 is "4 times faster than the iPad 2". This does not seem to be the case. If it were then, the Retina rendering (which is 4 pixels more) on the screen should be at least the same as the iPad 2 with the retina.

The same problem occurred when Apple released the iPhone 4. iPhone 4 was actually slower than 3GS and 3G.

CoreGraphics is slower on iPhone4 than on 3G / 3GS

To increase fps, you need to reduce the number of pixels that drawRect needs to be drawn. I suggest drawing CoreGraphics-related half-resolution stuff. Then take this context as a bitmap and enlarge it to full resolution. But only while the user drags.

+1
source

Source: https://habr.com/ru/post/1468984/


All Articles