我有一个绘图应用程序,我想在其中创建一个撤消方法。绘图发生在 TouchesMoved: 方法中。
我正在尝试创建一个 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。但我似乎无法弄清楚如何保存上下文,然后将其推回绘图表面。嗯。任何帮助都会……嗯……有帮助。提前致谢 -
而且,仅供参考,这是我在屏幕上绘制的内容,效果很好。这是在我的 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();