我知道这样的帖子被问了很多次,但我在谷歌和这个网站上搜索了很多次,都没有找到任何解决方案,这就是为什么我要贴这个问题。我想要撤消我绘制的CGContext线,而不使用UIBezierPath或其他任何东西。有什么办法吗?下面是我用来绘制的代码:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:mainImageView];
UIGraphicsBeginImageContext(mainImageView.frame.size);
[mainImageView.image drawInRect:CGRectMake(0, 0, mainImageView.frame.size.width, mainImageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dimension);
const CGFloat *components = CGColorGetComponents([color CGColor]);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;}谢谢。
发布于 2013-02-07 21:59:11
要添加撤消:
一旦图片被涂上,你就不能透露任何信息...
你要么必须在每次图像改变时保留图像的副本(大量的内存,尽管这可能通过只保留改变区域的副本来减少),要么保留用户绘制步骤的记录,然后在撤消之后重播它们。
发布于 2013-02-07 21:57:44
由于您正在绘制图像,因此您可以将图像置为空,可见的线条将消失。
mainImageView.image = NULL;
// Or
mainImageView.image = originalImage; // Where originalImage is your background if you're using one.更新
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:mainImageView];
UIGraphicsBeginImageContext(mainImageView.frame.size);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dimension);
const CGFloat *components = CGColorGetComponents([color CGColor]);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
// Add the line as a subView.
[mainImageView addSubView:[[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()]];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)undoLastLine
{
[mainImageView.subviews.lastObject removeFromSuperview];
}https://stackoverflow.com/questions/14752438
复制相似问题