0

I am simply trying to draw a bunch of lines to a view, depending on user interaction. For example, the user presses a button, and one line is drawn. The user presses another button, and another line is drawn, but the first one is still there, and so on. It seems like a trivial task, but from what I've been reading, non-destructive drawing is not very straightforward. Can anyone guide me in the right direction, ideally towards some sample code? Any kind of guidance is really appreciated though, of course. Literally just need to keep drawing various straight lines onto a view. Thanks in advance for any help!

Roman V
  • 21
  • 4
  • It looks like the topic was discussed here [link](http://stackoverflow.com/questions/865636/uiview-how-to-do-non-destructive-drawing) but I honestly can't say I understand the solution code. I don't see where the new drawing gets done over the existing image. – Roman V Mar 31 '11 at 20:06

2 Answers2

1

If you want to use Quartz Core, i believe you will have to re draw the whole thing every time...So someone adds a line, you draw the line, they add another line, you add both lines and so on...but you might be able to draw on layers and keep adding them on top of each other..

Daniel
  • 22,363
  • 9
  • 64
  • 71
  • Hmm, thanks. I'm really looking for a way not to have to re-draw the whole thing. There may be as many as a million lines in the view at a given time, with only 40-50 being drawn after each user interaction. I haven't tried but I'm guessing a million lines is not instant, and would really mess up the user experience. – Roman V Mar 31 '11 at 19:08
  • Well you can try adding layer on top of layers, or use opengl – Daniel Mar 31 '11 at 19:09
  • I have a feeling 1000 layers won't be too fast, and I'm trying to avoid opengl as a newbie, but maybe that's the way I'll have to end up going. – Roman V Mar 31 '11 at 19:23
1

If you don't need undo capability, try drawing to a CGBitmapContext, then creating an image from it and rendering that to the screen as needed. This is called "back buffering" (or "double buffering" or just "buffered drawing") and is very common.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • I don't think I need undo capability. I do need to erase lines every now and then but I was just going to paint background-colored lines over them. I tried to look up an example of what you're talking about and had trouble finding anything good (kept coming up with OpenGL stuff, which I'm trying to avoid as a newbie). Do I somehow get the current image at the beginning of drawRect, copy it into this CGBitmapContext, draw more stuff into it, then copy it onto the screen? Have you seen a sample of this anywhere? Call me crazy, but seems like such a trivial task I'm trying to accomplish. Thanks – Roman V Mar 31 '11 at 19:22