我有一个绘图应用程序,我想创建一个撤消方法。绘图发生在TouchesMoved:方法内部。保存CGContextRef
我想创建一个CGContextRef并将其推送到堆栈或将其保存在可以稍后恢复的上下文属性中,但没有任何运气。任何建议都会很棒。以下是我有...
UIImageView *drawingSurface;
CGContextRef undoContext;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawingSurface.image drawInRect:CGRectMake(0, 0, drawingSurface.image.size.width, drawingSurface.image.size.height)];
UIGraphicsPushContext(context);
// also tried but cant figure how to restore it
undoContext = context;
UIGraphicsEndImageContext();
}
然后我就用我的复原按钮,触发一个方法...
- (IBAction)restoreUndoImage {
UIGraphicsBeginImageContext(self.view.frame.size);
UIGraphicsPopContext();
drawingSurface.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
当我运行这一点,我相信我的drawingSurface被分配为零,因为它只是删除图像中的所有内容。
我的猜测是我不能用pop和push这种方式。但我似乎无法弄清楚如何保存上下文,然后将其推回到drawingSurface上。 Hmmmm。任何帮助将会......很好......有帮助。在此先感谢 -
而且,仅供参考,这是我正在做的画在屏幕上,这是伟大的工作。这是我的TouchesMoved内:
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawingSurface.image drawInRect:CGRectMake(0, 0, drawingSurface.image.size.width, drawingSurface.image.size.height)];
CGContextSetLineCap(context, kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(context, self.brush.size); // for size
CGContextSetStrokeColorWithColor (context,[currentColor CGColor]);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
drawingSurface.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
神奇的,深思熟虑的答案。不知道为什么OP不接受这一点。 – Tim 2012-05-31 21:35:44