首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >撤消CGContext行

撤消CGContext行
EN

Stack Overflow用户
提问于 2013-02-07 21:36:48
回答 2查看 724关注 0票数 0

我知道这样的帖子被问了很多次,但我在谷歌和这个网站上搜索了很多次,都没有找到任何解决方案,这就是为什么我要贴这个问题。我想要撤消我绘制的CGContext线,而不使用UIBezierPath或其他任何东西。有什么办法吗?下面是我用来绘制的代码:

代码语言:javascript
复制
 - (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;}

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2013-02-07 21:59:11

要添加撤消:

一旦图片被涂上,你就不能透露任何信息...

你要么必须在每次图像改变时保留图像的副本(大量的内存,尽管这可能通过只保留改变区域的副本来减少),要么保留用户绘制步骤的记录,然后在撤消之后重播它们。

票数 1
EN

Stack Overflow用户

发布于 2013-02-07 21:57:44

由于您正在绘制图像,因此您可以将图像置为空,可见的线条将消失。

代码语言:javascript
复制
mainImageView.image = NULL;
// Or
mainImageView.image = originalImage; // Where originalImage is your background if you're using one.

更新

代码语言:javascript
复制
- (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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14752438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档