I'm trying to draw a procedurally generated tree in a custom view's drawRect
method. This is perfectly snappy up to about 20k lines. Beyond that point and drawing performance goes entirely into the crapper (i.e. nearly 1000ms to draw the tree).
The drawing code for drawing the tree is:
func drawBranch( branch: Branch, context :CGContext ) {
CGContextMoveToPoint(context, (branch.originPoint().x), (branch.originPoint().y))
CGContextAddLineToPoint(context, (branch.endPoint().x), (branch.endPoint().y))
for b in branch.children {
drawBranch(b, context: context)
}
}
override func drawRect(rect: CGRect) {
print("\(NSDate()) drawRect")
// Setup graphics context
let ctx = UIGraphicsGetCurrentContext()
// clear context
CGContextClearRect(ctx, rect)
CGContextSetRGBFillColor(ctx, 0.0, 0.0, 0.0, 1.0)
CGContextFillRect(ctx, rect)
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0.0, rect.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextSetShouldAntialias(ctx, true)
CGContextSetRGBStrokeColor(ctx, 0.3, 0.3, 0.3, 1)
if let tree = self.tree {
drawBranch(tree, context: ctx)
}
CGContextStrokePath(ctx)
CGContextRestoreGState(ctx);
}
Is there any hope for this ever being fast, or should I have given up a long time ago and moved on to some other rendering method/engine?
I read through this question here which seems to imply that it's never going to work, but that was 3 years ago and I'm wondering if anything has changed since then.