Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawrect with CGBitmapContext is too slow
    text
    copied!<p>So I've got a basic drawing app in the process that allows me to draw lines. I draw to an off screen bitmap then present the image in <code>drawRect</code>. It works but its way too slow, updating about half a second after you've drawn it with your finger. I took the code and adapted it from this tutorial, <a href="http://www.youtube.com/watch?v=UfWeMIL-Nu8&amp;feature=relmfu" rel="nofollow">http://www.youtube.com/watch?v=UfWeMIL-Nu8&amp;feature=relmfu</a> , as you can see in the comments people are also saying its too slow but the guy hasn't responded.</p> <p>So how can I speed it up? or is there a better way to do it? any pointers will be appreciated.</p> <p>Heres the code in my <code>DrawView.m</code>. </p> <pre><code>-(id)initWithCoder:(NSCoder *)aDecoder { if ((self=[super initWithCoder:aDecoder])) { [self setUpBuffer]; } return self; } -(void)setUpBuffer { CGContextRelease(offscreenBuffer); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); offscreenBuffer = CGBitmapContextCreate(NULL, self.bounds.size.width, self.bounds.size.height, 8, self.bounds.size.width*4, colorSpace, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorSpace); CGContextTranslateCTM(offscreenBuffer, 0, self.bounds.size.height); CGContextScaleCTM(offscreenBuffer, 1.0, -1.0); } -(void)drawToBuffer:(CGPoint)coordA :(CGPoint)coordB :(UIColor *)penColor :(int)thickness { CGContextBeginPath(offscreenBuffer); CGContextMoveToPoint(offscreenBuffer, coordA.x,coordA.y); CGContextAddLineToPoint(offscreenBuffer, coordB.x,coordB.y); CGContextSetLineWidth(offscreenBuffer, thickness); CGContextSetLineCap(offscreenBuffer, kCGLineCapRound); CGContextSetStrokeColorWithColor(offscreenBuffer, [penColor CGColor]); CGContextStrokePath(offscreenBuffer); } - (void)drawRect:(CGRect)rect { CGImageRef cgImage = CGBitmapContextCreateImage(offscreenBuffer); UIImage *image =[[UIImage alloc] initWithCGImage:cgImage]; CGImageRelease(cgImage); [image drawInRect:self.bounds]; } </code></pre> <p>Works perfectly on the simulator but not device, I imagine that's something to do with processor speed.</p> <p>I'm using ARC.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload