I'm in the process of making a simple drawing program for the iPhone. At the moment, touch events add their location to a list which is connected with a bunch of CGContextAddLineToPoint calls which are redrawn in every call to drawRect.
I'm running into performance issues at fairly low numbers of lines (they are transparent, though), so I tried drawing everything into a CGLayer. Now, instead of drawing each line every frame, it draws each line only once and draws the CGLayer to the screen every frame.
CGContextRef backgroundContext = CGLayerGetContext(pathBuffer);
CGContextSetLineWidth(backgroundContext, 2.0);
CGContextSetLineCap(backgroundContext, kCGLineCapButt);
CGContextSetStrokeColorWithColor(backgroundContext, [pathColor CGColor]);
if([[touchPoints objectAtIndex:0] continuesToNextPoint])
{
CGContextMoveToPoint(backgroundContext, [[touchPoints objectAtIndex:0] point].x, [[touchPoints objectAtIndex:0] point].y);
CGContextAddLineToPoint(backgroundContext, [[touchPoints objectAtIndex:1] point].x, [[touchPoints objectAtIndex:1] point].y);
CGContextStrokePath(backgroundContext);
}
//remove it from touchpoints
[touchPoints removeObjectAtIndex:0];
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawLayerAtPoint(context, CGPointMake(0, 0), pathBuffer);
This, if anything, is SLOWER than what I was doing before. Am I doing something wrong? Would I be better served drawing to a UIImage or something? Thanks.