views:

1393

answers:

3

Everytime UIView's drawRect is called, the content drawn by previous drawRect (using Core Graphics) is cleared out. How can I make it so that the paths rendered from previous drawRect calls stay until I explicitly clear it?

+2  A: 

You basically need to clip the 'dirty' part of your rect where changes have been made, and only this part will be re-drawn.

- (void)setNeedsDisplayInRect:(CGRect)invalidRect
alamodey
The issue is not that rect is not redrawn but rather when drawRect is reissued, it clears out all the previous drawn paths. It seems like that's the default behavior with Core Graphic's drawing routine.
Boon
A: 

I'm pretty certain there's no way around this; it's how UIView is designed to work. If asked to, your custom view should be able to draw any part of itself at any time. This is partly because views can do more than just appear onscreen. e.g. on the desktop they can be printed, and even on the iPhone, you might wish to capture the contents of a view to a bitmap.

Mike Abdullah
A: 

I'm fighting this issue myself right now. The problem is there is a property on the UIView called "clearsContextBeforeDrawing" that according to the documentation is supposed to fix this problem, however it doesn't work that way in my experience.

I think ultimately the solution to this is going to be to allocate an offscreen buffer and do all my drawing there, then blt it over to the UIView in the drawRect method.

Lounges
Yes, that also raises OTHER problems: one is that iPhone/iPad only supports RGBA and not RGB, so updating the screen requires RGBA->RGB conversion (which takes 99% of the time.) Plus, it will still clear the UIView in vain, for absolutely no purpose. If anybody knows a way around these problems, please ping!
gilm