So suppose you had to draw 100,000 lines onto a UIView that's maybe 3,000 x 3,000 in size. It sits inside of a UIScrollView, by the way. I did this the regular way with Quartz-2D, writing a regular drawRect, and it takes way too long (a couple of minutes on the latest hardware). I have done some research and there seem to be 2 faster methods:
1) Use Quartz-2D, but draw to an offscreen buffer, then load onto the UIView
2) Use OpenGL ES, draw the lines, print to a UIImage, and load onto the UIView
Do you have any sense of the difference in performance between the original and these 2 methods? Do you have any sample code I could use to accomplish either (1) or (2)? It seems like a straightforward task, but I'm a bit lost.
For option (1), here is my attempt from what I could find online:
- (CGContextRef) createOffscreenContext: (CGSize) size {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8,
size.width*4, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
return context;
}
- (void)drawRect:(CGRect) rect {
self.backgroundColor = [UIColor clearColor];
CGSize size = self.frame.size;
drawingContext = [self createOffscreenContext: size];
UIGraphicsPushContext(drawingContext);
//draw stuff here
CGImageRef cgImage = CGBitmapContextCreateImage(drawingContext);
UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];
UIGraphicsPopContext();
CGImageRelease(cgImage);
[uiImage drawInRect: rect];
[uiImage release];
}
Thoughts? Any help and/or advice is appreciated.
UPDATE: I implemented the 1st solution and it is in fact a ton faster. However it makes the program pretty unstable, and sometimes calling setNeedsDisplay crashes it. Will try to work on an OpenGL solution next.