我用过Quartz2D画线,现在用UNDO绘制线?

时间:2013-11-23 07:26:39

标签: iphone ios6 quartz-2d ipad-2

我正在使用Quartz2D来绘制这些东西,我已经遵循了这个tutorial并且它正常工作。但我需要在其中实现UNDO选项。

当我按下它时,我有一个撤消按钮,它必须撤消绘制的线。

我使用下面的代码绘制。是否有人知道它的解决方案。

提前完成。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];

UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();

lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

if(!mouseSwiped) {
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

UIGraphicsBeginImageContext(self.mainImage.frame.size);
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempDrawImage.image = nil;
UIGraphicsEndImageContext();
}

2 个答案:

答案 0 :(得分:0)

您无法撤消特定的绘图操作。您必须保存所有触摸并以适当的方式存储它(您的“步骤”对象)。您将此“步骤”对象存储在绘图会话(数组)中,如果要执行撤消操作,则删除此数组中的最后一个对象,然后重绘整个图像,并在(会话)数组中保留所有步骤

答案 1 :(得分:0)

最简单的撤消方法是捕获即将更改的矩形,并将该矩形的内容存储在内存或文件中。如果要撤消,只需使用kCGBlendModeCopy绘制以正确坐标保存的矩形。

对于多次撤消,您可以存储这些矩形的堆栈并在要撤消时弹出。重做也很容易,而不是弹出,你只需向后移动一个阵列中的一个位置。对于重做,你向前移动一个位置。

如果您正在实时修改图像(即用手指绘图),那么您无法预先获取矩形,而是需要第二个缓冲区,其中包含您的副本图像,您可以使用它来完成绘制操作后的撤消矩形。完成后,将图像复制到撤消缓冲区。

祝你好运!