简体   繁体   English

如何将NSUndoManager与UIImageView或CGContext一起使用

[英]How to use NSUndoManager with a UIImageView or CGContext

I have an app that uses cgcontext to draw things onto a UIImageView that the user draws using touch events, and I want to be able to undo the drawings made by the user. 我有一个应用程序,它使用cgcontext将事物绘制到用户使用触摸事件绘制的UIImageView上,并且我希望能够撤消用户所做的绘图。

Edit: 编辑:


I am trying to use my touchesBegan to save theUIImage at the begining of a touch to the NSUndoManager 我试图使用我的touchesBegan在触摸NSUndoManager时保存NSUndoManager


And then how do I use it to undo using a UIAlertView. 然后我如何使用它来撤消使用UIAlertView。
Meaning, I want to call a method from my UIAlertView that undo's then another that will redo, but I don't know how to accomplish this. 意思是,我想从我的UIAlertView中调用一个方法来撤消另一个将重做的方法,但我不知道如何实现这一点。
Please help 请帮忙

I write graphics software for the iPhone ( http://www.layersforiphone.com/ ) and I spent a while trying to figure this out. 我为iPhone编写了图形软件( http://www.layersforiphone.com/ ),我花了一些时间试图解决这个问题。 The problem is, CGContexts aren't inherently "undoable." 问题是,CGContexts本身并不是“可撤销的”。 You can't unfill a rectangle or restore a portion you've drawn over. 您无法取消填充矩形或恢复您绘制的部分。 The data just isn't there. 数据不存在。 In my experience, the best bet is to create a "save" method that looks at the region being altered and saves the image data in that region to the undo stack before drawing operations are performed. 根据我的经验,最好的办法是创建一个“保存”方法,查看要更改的区域,并在执行绘图操作之前将该区域中的图像数据保存到撤消堆栈。 Then, when you decide to undo, you can take the modified region and restore it from your saved data. 然后,当您决定撤消时,您可以获取修改后的区域并从保存的数据中恢复它。

I chose to implement that approach independently of NSUndoManager in Layers because you basically need a stack of CGImageRefs and CGRects and it doesn't map well to the NSUndoManager. 我选择独立于层中的NSUndoManager实现该方法,因为您基本上需要一堆CGImageRefs和CGRects,并且它不能很好地映射到NSUndoManager。

Note: Technically, you could just save a copy of the entire image each time it is modified. 注意:从技术上讲,您可以在每次修改时保存整个图像的副本。 This is a bad use of memory, though - since you may change only a small area. 但这对记忆的使用很糟糕 - 因为你可能只改变一小块区域。 If you want to have a generous undo history (10+ steps), it's definitely worth your while to save and restore small subimages within your drawing space. 如果你想拥有大量的撤消历史记录(10个以上的步骤),那么在你的绘图空间中保存和恢复小的子图像绝对是值得的。

Hope that helps! 希望有所帮助! I know it's not a great solution - you really can't do much with the built-in APIs. 我知道这不是一个很好的解决方案 - 你内置的API实际上做不了多少。 If you create a general solution to this problem I think a lot of people would be interested in it, though! 如果你为这个问题创建一个通用的解决方案,我想很多人会对它感兴趣!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM