保存和恢复CGContext
我试图保存和恢复 CGContext 以避免再次进行繁重的绘图计算,但收到错误
。
我做错了什么?这样做的正确方法是什么?
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
if (initialized) {
CGContextRestoreGState(context);
//scale context
return;
}
initialized = YES;
//heavy drawing computation and drawing
CGContextSaveGState(context);
}
I'm trying to save and restore a CGContext to avoid doing heavy drawing computations for a second time and I'm getting the error <Error>: CGGStackRestore: gstack underflow
.
What am I doing wrong? What is the correct way to do this?
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
if (initialized) {
CGContextRestoreGState(context);
//scale context
return;
}
initialized = YES;
//heavy drawing computation and drawing
CGContextSaveGState(context);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您可能误解了 CGContextSaveGState() 和 CGContextRestoreGState() 的作用。它们将当前图形状态推入堆栈并将其弹出,让您可以变换当前绘图空间、更改线条样式等,然后将状态恢复到设置这些值之前的状态。它不存储绘图元素,例如路径。
来自 关于 CGContextSaveGState() 的文档:
图形状态堆栈应在
drawRect:
开始时重置,这就是当您尝试从堆栈中弹出图形状态时出现错误的原因。由于您没有推入任何一个,因此没有任何一个可以弹出。所有这些意味着您无法将绘图作为图形状态存储在堆栈上,然后在以后恢复。如果您所担心的只是缓存您的绘图,那么支持您的
UIView
(在 iPhone 上)的CALayer
可以为您完成此操作。如果您所做的只是移动视图,则不会重新绘制视图。只有当您手动告诉它这样做时,它才会被绘制。如果您确实必须更新绘图的一部分,我建议将静态元素拆分到它们自己的视图或 CALayers 中,以便仅重绘更改的部分。I think you might be misinterpreting what
CGContextSaveGState()
andCGContextRestoreGState()
do. They push the current graphics state onto a stack and pop it off, letting you transform the current drawing space, change line styles, etc., then restore the state to what it was before you set those values. It does not store drawing elements, like paths.From the documentation on CGContextSaveGState():
The graphics state stack should be reset at the start of your
drawRect:
, which is why you're getting errors when you try to pop a graphics state off the stack. Since you hadn't pushed one on, there was none to pop off. All of this means that you can't store your drawing as graphics state on the stack, then restore it later.If all you are worried about is caching your drawing, that is done for you by the
CALayer
that backs yourUIView
(on the iPhone). If all you are doing is moving your view around, it won't be redrawn. It will only be drawn if you manually tell it to do so. If you do have to update part of the drawing, I recommend splitting the static elements off into their own views orCALayers
so that only the part that changes is redrawn.您不想先保存再恢复吗?如果您在保存之前进行恢复,则没有要恢复的上下文,并且您会遇到下溢。
这是我使用它的方式:
或者:
也许你正在尝试做其他事情。
Don't you want to Save first and then Restore? If you are restoring before a save, then there is no context to restore, and you'd get an underflow.
Here is the way I have used it:
or:
Maybe you are trying to do something else.
..根据你的代码!,
看来您正在保存上下文之前恢复它。
第一件事首先:
Pop
Store(push)
的一般规则必须有 < code>Restore(pop)CGCreate
、CGCopy
、Sample代码:
.. Based on your code !,
It seems that you are Restoring the Context before Saving it.
First Thing First:
Pop
Store(push)
there must beRestore(pop)
CGCreate
,CGCopy
,Sample code: