Reputation: 1183
I have painting app which works like that:
And I want to implement undo method. Firstly I thought I could use brushAccumulator.image (CIImage) as undo object (add it to mutableArray, then when undo method is invoked set brushAccumulator image to one of mutableArray objects) but I found that: A CIImage is not an image that contains pixels, it is simply the result of a series of instructions to build it, the output of a CIFilter for example. So if you copy the CIImage you just copy those instructions, which when modified would modify the output.
So I thought I could make NSBitmapImageRep from brushAccumulator's image and store it to NSMutableArray. But I met problems with updating brushAccumulator. I set the new CIImage which is made from one of NSBitmapImageRep from NSMutableArray as brushAccumulator image, but brushAccumulator image doesn't changes.
What could you offer me to achieve undo/redo effect, while my painting app is based on CIImageAccumulator (similar to CIMicroPaint sample code)?
Upvotes: 0
Views: 506
Reputation: 2916
Here's a general algorithm that works for me. You'll need to copies of your image: 1) The one you are drawing directly to- "direct bitmap" 2) A copy of what it looks like before the draw operation- "undo bitmap"
So you draw to the direct, and take note of what the change frame is ("changeFrame") Then you copy out the changeFrame from the undo context to be used later on for an undo (and store the changeFrame along with it in some object- SOChangeBitmap). Then you pull an image from changeFrame of the direct bitmap, and replace the pixels in the undo bitmap w/ the change frame.
Then, to undo, you take the SOChangeBitmap (image + changeFrame) and apply it to the direct bitmap (and update the undo bitmap as well).
Upvotes: 0