尝试撤消上下文时发生错误 [英] error when trying to undo Context

查看:71
本文介绍了尝试撤消上下文时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从保存状态
中撤消myContext,然后在画完线后调用undo方法将我的上下文恢复为先前状态,但报告错误

 < Error> ;: CGContextRestoreGState:无效上下文

code

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

UITouch * touch = [触摸anyObject];
CGPoint currentPoint = [触摸locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineCap(context,kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),1.0,0.0,0.0,1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(),lastPoint.x,lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),currentPoint.x,currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}

-(void)undo {
CGContextRestoreGState(context);
}


解决方案

从我的信中问题,我想您正在尝试实施撤消,对吗? CGContextSaveGState CGContextRestoreGState 与此无关。





也就是说,您还引用了一个已被长期破坏的上下文。一旦调用 UIGraphicsEndImageContext(); ,以前存储在 context 变量中的上下文就消失了。这就是打印错误的原因。



要执行撤消操作,您可能必须存储生成的图像或用户执行的操作或其他的东西。 CGContexts不会帮助您...


i try to undo myContext from save state then after i draw the line i call undo method to restore my context to previous but it report error

<Error>: CGContextRestoreGState: invalid context

code

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

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
}

- (void)undo {
    CGContextRestoreGState(context);
}

解决方案

From what I read from you question, I guess that you're trying to implement undo, right? CGContextSaveGState and CGContextRestoreGState have nothing to do with that.

These two methods only store meta-data of the context in the context. Metadata like the current drawing color, coordinate-system transformation, line thickness etc. This GState you preserve using these methods allows you to undo settings of a context, not it's content. You have to do undo in a different way...

That said, you also refer to a context after it has been destroyed for a long time. As soon as you call UIGraphicsEndImageContext();, the context you previously stored in the context variable is gone. And that's the reason why the error is being printed.

In order to do undo, you'd probably have to store the image you generated or the actions the user did or something else. CGContexts won't help you there...

这篇关于尝试撤消上下文时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆