0

I'm using the following code to create a persistent DrawRect for a finger painting type exercise

When I run on the simulator it is fine, when I run on the iPad it gets delayed turning curves in jagged lines Please can some one help optimize or suggest a better method

Thanks in advance

 - (void)drawRect:(CGRect)rect
 {
 UIGraphicsPushContext(drawingContext);
 CGImageRef cgImage = CGBitmapContextCreateImage(drawingContext); 
 image = [[UIImage alloc] initWithCGImage:cgImage];

 if (draw) {
 CGContextSetLineWidth(drawingContext, (2.0 * size) );
 CGContextSetStrokeColorWithColor(drawingContext, color);
 CGContextSetLineCap(drawingContext,kCGLineCapRound);
 CGContextMoveToPoint(drawingContext,    lastPt.x - (31.0 / self.transform.a),  lastPt.y - (31.0 / self.transform.a)  );
 CGContextAddLineToPoint(drawingContext, currPt.x - (31.0 / self.transform.a),  currPt.y - (31.0 / self.transform.a)  );
 CGContextStrokePath(drawingContext);
 } else {
 //                     CGContextSetFillColorWithColor(drawingContext, [[UIColor redColor] CGColor]);
 //                     CGContextFillRect(drawingContext,    CGRectMake( currPt.x - (31.0 / self.transform.a) - 16.0 , currPt.y - (31.0 / self.transform.a) - 16.0 , 40.0 , 40.0) );
 CGContextClearRect(drawingContext, CGRectMake( currPt.x - (31.0 / self.transform.a) - 16.0 , currPt.y - (31.0 / self.transform.a) - 16.0 , 40.0 , 40.0) );
 }

 UIGraphicsPopContext();
 CGImageRelease(cgImage);
 NSLog(@"    %@   "  ,  rect);
 [image drawInRect: rect];

 lastPt = currPt;
 }
BarryF
  • 77
  • 1
  • 10

1 Answers1

0

I found changing the call from

[self setNeedsDisplay]

to

[self setNeedsDisplayInRect]

change from 1fps to 52fps according to Instruments

Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50
BarryF
  • 77
  • 1
  • 10