如何将NSUndoManager与UIImageView或CGContext一起使用 [英] How to use NSUndoManager with a UIImageView or CGContext

查看:84
本文介绍了如何将NSUndoManager与UIImageView或CGContext一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序使用cgcontext将内容绘制到用户使用触摸事件绘制的UIImageView上,并且我希望能够撤消用户所做的绘制。

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.

编辑:

我正在尝试使用touches开始将UIImage保存为开始接触 NSUndoManager

I am trying to use my touchesBegan to save theUIImage at the begining of a touch to the NSUndoManager



的意思是,我想从UIAlertView调用一个方法,该方法先撤消,然后再做一次重做,但我不知道该如何完成。

请帮助

And then how do I use it to undo using a 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.
Please help

推荐答案

我为iPhone编写了图形软件( http://www.layersforiphone.com/ ),我花了一段时间试图解决这个问题。问题在于,CGContext本质上不是不可撤销的。您无法取消填充矩形或恢复已绘制的部分。数据不存在。以我的经验,最好的选择是创建一个保存方法,该方法查看正在更改的区域,然后在执行绘制操作之前将该区域中的图像数据保存到撤消堆栈中。然后,当您决定撤消操作时,可以采用修改后的区域并从保存的数据中还原它。

I write graphics software for the iPhone (http://www.layersforiphone.com/) and I spent a while trying to figure this out. The problem is, CGContexts aren't inherently "undoable." 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.

我选择独立于Layers中的NSUndoManager来实现该方法,因为您基本上需要一堆CGImageRefs和CGRects,并且不能很好地映射到NSUndoManager。

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.

注意:从技术上讲,您每次只能保存整个图像的副本它被修改。但是,这是对内存的不好使用-因为您只能更改很小的区域。如果您想拥有丰富的撤消历史记录(超过10个步骤),那么绝对值得在绘图空间中保存和恢复小的子图像。

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.

希望如此!我知道这不是一个很好的解决方案-内置API实在不能做太多事情。但是,如果您为这个问题创建一个通用的解决方案,我想很多人会对它感兴趣!

Hope that helps! I know it's not a great solution - you really can't do much with the built-in APIs. If you create a general solution to this problem I think a lot of people would be interested in it, though!

这篇关于如何将NSUndoManager与UIImageView或CGContext一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆