Reputation: 1197
I have a loop in which I'm adding many (10000+) lines to a UIBezierPath. This seems to be fine, but once I try and render the bezierpath, my device becomes extremely slow and jerky. Is this because I've added too many lines to my path?
Adding lines to UIBezierPath - simplified: (this seems fine)
[path moveToPoint:CGPointZero];
for (int i = 0; i < 10000; i++ ) {
[path addLineToPoint:CGPointMake(i, i)];
}
Rendering BeizerPath (Suggested by Rob) - this seems slow.
- (void)drawBezierAnimate:(BOOL)animate
{
UIBezierPath *bezierPath = path;
CAShapeLayer *bezier = [[CAShapeLayer alloc] init];
bezier.path = bezierPath.CGPath;
bezier.strokeColor = [UIColor blueColor].CGColor;
bezier.fillColor = [UIColor clearColor].CGColor;
bezier.lineWidth = 2.0;
bezier.strokeStart = 0.0;
bezier.strokeEnd = 1.0;
[self.layer addSublayer:bezier];
if (animate)
{
CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animateStrokeEnd.duration = 100.0;
animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f];
animateStrokeEnd.toValue = [NSNumber numberWithFloat:1.0f];
[bezier addAnimation:animateStrokeEnd forKey:@"strokeEndAnimation"];
}
}
Qs:
1) Is this because I'm adding too many paths too quickly?
2) I want to eventually draw many different lines of different colors, so I assume I would need to create multiple (10000+) UIBezierPaths - would this help or greatly slow the device as well?
3) How would I get around this?
Thanks in advance for your help.
Upvotes: 2
Views: 1924
Reputation: 321
I have discovered that UIBezierPath is slow because of antialiasing. My solution is to turn off antialiasing during various dragging operations, and after the gesture is finished redraw with antialiasing enabled.
You can do this using CGContextSetShouldAntialias(context, ...);
Upvotes: 3
Reputation: 25632
I don't think there's a way to get this run quick enough.
It might work for a one-time render (to an image buffer). But with animation - no.
The documentation doesn't put a limit on the number of points within a path, but with thousands of them I think you are way over any reasonable limit.
OpenGL might be useful here - maybe someone can chime in and give some advise on this.
Using UIKit alone, you probably will have to compose your image from several cache layers.
Upvotes: 2