0

I'm using

Where penLayer is a UIView

UIGraphicsBeginImageContext(penLayer.frame.size);

CGContextRef  context = UIGraphicsGetCurrentContext();
    //My Drawing Code
penLayer.image = UIGraphicsGetImageFromCurrentImageContext();
[penLayer setNeedsDisplay];

This draws great but each time I call drawRect it wipes out what was before Currently I'm having to save all drawing data to a dictionary

Is there anyway to prevent it from clearing out with each call to drawRect and perhaps have a UIButton that calls clear when needed ?

Thanks

STW
  • 44,917
  • 17
  • 105
  • 161
BarryF
  • 77
  • 1
  • 10

1 Answers1

0

There is a BOOL property on UIView called clearsContextBeforeDrawing. I'm assuming that you haven't used it. From the discussion in the documentation:

When set to YES, the drawing buffer is automatically cleared to transparent black before the drawRect: method is called. This behavior ensures that there are no visual artifacts left over when the view’s contents are redrawn. If the view’s opaque property is also set to YES, the backgroundColor property of the view must not be nil or drawing errors may occur. The default value of this property is YES.

If you set the value of this property to NO, you are responsible for ensuring the contents of the view are drawn properly in your drawRect: method. If your drawing code is already heavily optimized, setting this property is NO can improve performance, especially during scrolling when only a portion of the view might need to be redrawn.

Community
  • 1
  • 1
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205