iPhone - trying to figure out the drawRect:
<p>I have this app of mine that contains a section that will allow the user to do simple paintings on the screen. My idea is to offer two kind of painting: rough and smooth (bezier) lines. </p>

<p>How it is built: I have a transparent UIView with a custom drawRect method. I am not sure if I understood drawRect completely, but the code is partially working.</p>

<p>How it works: the user moves the finger around the screen. When he first touches the screen, TouchesBegan creates a mutablearray called myPoints and store the coordinate of the first point and stores the canvas as it is (to allow undo).</p>

<p>After that, TouchesMoved routine grabs all points and adds them to myPoints. This array is needed in the case the user chooses to draw bezier lines. </p>

<p>When touchesEnded is detected, the app has to see if the user was painting a regular line (rough) or a bezier. If it was not bezier, the lines are already drawn. If a bezier is needed, I need to erase the canvas, restore the canvas as it was when TouchesBegan stored it, calculate the new bezier approximation (reverse engineered to be the best bezier curve to fit the drawn points) and draw the new curve, replacing the rough one.</p>

<p>These are my problems:</p>

<p>1) How to store the undo? I first thought on creating a new layer to store the undo, but doing that increased the app memory usage from 8 to 13 Mb... I am not sure if this is a safe level to use (the app is compatible with 3GS, iPod Touch 2nd G, and up, including iPad). Then, I decided to fire a queue operation to save the canvas. This is resulting in blank images being saved (I suppose the saving method is getting nil for the context, because it cannot get the context outside the drawRect).</p>

<p>2) the second problem is similar to the first one. At this point I need to restore the canvas to what it was when it was saved, but as the restore method is outside the drawRect it it getting nil to the context too.</p>

<p>TouchesBegan, TouchesMoved and TouchesEnded and all methods mentioned are inside the same class where the drawRect is, not in the viewController.</p>

<p>My questions is: how do you guys would do that? </p>

<p>I saw a painting app on the store offering multi level undo. I cannot imagine how they did that, because in my first try, for ever layer I created I was consuming 5 Mb of memory, so it appears impossible to have that kind of undos... </p>

<p>please, help me... I am desperate!</p>

<p>thanks  </p>
