【问题标题】:I have used Quartz2D to draw the line, Now to UNDO the drawn line?我用 Quartz2D 画线,现在要 UNDO 画线吗?
【发布时间】:2013-11-23 07:26:38
【问题描述】:

好吧,我正在使用 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();
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      执行撤消的最简单方法是捕获即将更改的矩形并将该矩形的内容存储在内存或文件中。当您想要撤消时,只需使用 kCGBlendModeCopy 绘制保存在正确坐标处的矩形即可。

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

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

      祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-24
        • 2019-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-12
        • 1970-01-01
        相关资源
        最近更新 更多