How to use NSUndoManager with UIImageView or CGContext

I have an application that uses cgcontext to draw things in a UIImageView, which the user draws using touch events, and I want to be able to undo the drawings made by the user.

Edit:


I'm trying to use my touchhesBegan to save theUIImage at the beginning of touching NSUndoManager


And then how to use it to cancel the use of UIAlertView.
Meaning, I want to call a method from my UIAlertView that cancels, and then another that will be repeated, but I don’t know how to do this.
Please, help

+4
source share
1 answer

I am writing graphics software for the iPhone ( http://www.layersforiphone.com/ ) and I spent some time trying to figure it out. The problem is that CGContexts are not inherently β€œexcellent”. You cannot fill the rectangle or restore the part that you drew. There is simply no data. In my experience, it’s best to create a β€œsave” method that looks at the changed region and saves the image data in that region onto the undo stack before drawing operations are performed. Then, when you decide to cancel, you can take the changed area and restore it from the saved data.

I decided to implement this approach independently of NSUndoManager in layers, because you basically need a stack of CGImageRefs and CGRects, and it does not render NSUndoManager well.

Note. Technically, you can simply save a copy of the entire image each time it is resized. This is a poor use of memory, though - since you can only change a small area. If you want to have a generous cancellation history (more than 10 steps), you should definitely save and restore small subimases in your drawing space.

Hope this helps! I know this is not a great solution - you really can't do much with the built-in APIs. If you create a common solution to this problem, I think many people will be interested in this, though!

+3
source

All Articles