UIView with drawRect / update image performance issue on Retina displays

Shortly speaking:

LooksLike drawRectitself (even empty) leads to significant performance bottlenecks depending on the resolution of the device - a larger screen is worse. Is there a way to speed up the redrawing of view content?

More details:

I am creating a small drawing application on iOS - the user moves his finger on the screen to draw a line.

The idea of ​​this is quite simple - while the user moves his finger touchesMoved, accumulates changes to the on-screen buffer image and invalidates the view in order to combine the off-screen buffer with the contents of the view.

A simple piece of code might look like this:

@interface CanvasView : UIView
...
end;


@implementation CanvasView{
    UIImage *canvasContentImage;
    UIImage *bufferImage
    CGContextRef drawingContext;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    // prepare drawing to start
    UIGraphicsBeginImageContext(canvasSize);
    drawingContext = UIGraphicsGetCurrentContext();
    ...

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    // draw to bufferImage
    CGContextMoveToPoint(drawingContext, prevPoint.x, prevPoint.y);
    CGContextAddLineToPoint(drawingContext, point.x, point.y);
    CGContextStrokePath(drawingContext);
    ...
    [self setNeedDisplay];
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //finish drawing
    UIGraphicsEndImageContext();
    //merge canvasContentImage with bufferImage
    ...
}

-(void)drawRect:(CGRect)rect{
    // draw bufferImage - merge it with current view content

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, self.bounds, canvasContentImage.CGImage);
    CGContextDrawImage(context, imageRect, bufferImage.CGImage);
    ...

}

fps.

, ​​, 60 . fps . , iPad Retina 15-20 , .

, , setNeedsDisplay , . setNeedsDisplayInRect, . , ( ).

, , . , fps 15-20 - , . , drawRect, fps 60. , , . , :

//    -(void)drawRect:(CGRect)rect{
//        // draw bufferImage - merge it with current view content
//        ...
//    }

, drawRect touchMoved, , drawRect - 60 . , drawRect .

, , pregenerated drawRect:

" drawRect: . ".

, , drawRect , " "

, :

  • drawRect, , drawRect - ?

  • , ? OpenGL , /, .

. !

EDIT:

UIImageView . ( 19-22 ). 50-60 . , , - - , 60 . UIImageView.image, , fps 19-22. , , .

, - (UIImageView), ?

+4
1

, ( ) . 30 iPad, .

, , :

  • UIImageView
  • UIImageView.image - UIView setNeedsDisplay/setNeedsDisplayInRect.
  • [self performSelector:@selector(setImage:) withObject:img afterDelay:0]; UIImageView.image = img, UIImageView.

- , ( ).

, , performSelector fps , , setImage , . , - , .

+1

All Articles