- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图保存并恢复 CGContext 以避免再次进行繁重的绘图计算,但出现错误 <Error>: CGGStackRestore: gstack underflow
。
我做错了什么?正确的做法是什么?
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
if (initialized) {
CGContextRestoreGState(context);
//scale context
return;
}
initialized = YES;
//heavy drawing computation and drawing
CGContextSaveGState(context);
}
最佳答案
我认为您可能误解了 CGContextSaveGState()
和 CGContextRestoreGState()
的作用。它们将当前图形状态插入堆栈并将其弹出,让您可以变换当前绘图空间、更改线条样式等,然后将状态恢复到设置这些值之前的状态。它不存储绘图元素,例如路径。
来自documentation on CGContextSaveGState() :
Each graphics context maintains a stack of graphics states. Note that not all aspects of the current drawing environment are elements of the graphics state. For example, the current path is not considered part of the graphics state and is therefore not saved when you call the
CGContextSaveGState()
function.
图形状态堆栈应在 drawRect:
开始时重置,这就是当您尝试从堆栈中弹出图形状态时出现错误的原因。由于您没有插入任何一个,因此没有任何一个可以弹出。所有这些意味着您无法将绘图作为图形状态存储在堆栈上,然后在以后恢复。
如果您所担心的只是缓存您的绘图,那么支持您的 UIView
(在 iPhone 上)的 CALayer
可以为您完成此操作。如果您所做的只是移动 View ,则不会重新绘制 View 。只有当您手动告诉它这样做时,它才会被绘制。如果您确实必须更新绘图的一部分,我建议将静态元素拆分到它们自己的 View 或 CALayers 中,以便仅重绘更改的部分。
关于iphone - 保存和恢复CGContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1446446/
我在为 MKMapOverlayView 创建的 CGContext 中执行一些 CG 绘图操作。在我的上下文中绘制之后,我创建了一个图像并将其粘贴到 MapKit 提供的上下文中。 - (void)
我有一个 CGBitmapContext (bitmapContext),我想将它的一些矩形部分 (rect) 绘制到当前 CGContext (context) >). 现在我就是这样做的: CGC
各位,这是我的代码: - (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx { //backgrou
我正在使用代码在 CGContext 上显示 pdf 页面 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
在我的 iPhone 项目中,我有一个 UIView,我在其中实现了 drawRect 方法: - (void)drawRect:(CGRect)rect { CGContextRef con
我正在尝试在 UIView draw(_:) 中创建自己的 CGContext。这里: override func draw(_ rect: CGRect) { //let c
我正在UIView drawRect函数中绘制一个形状,该形状涉及剪切路径,然后在后面添加彩色块,以便颜色具有剪切路径的形状。但是,由于某些原因,路径的线条无法顺利伸出;如果抗锯齿功能无法正常使用,
我正在使用以下代码缩小UIImage: let image = UIImage(contentsOfFile: imageUrl.path!)!.CGImage let
我正在尝试使用 Core Graphics 从另一个线程在自定义 UIView 中进行无限绘图。无法获取当前的 CGContext,因此我尝试创建一个新的。但我不知道如何将其应用到 View 中。 这
在测试我的应用程序是否存在任何错误时,我注意到在模拟器中,当我调用其中一个函数将图像设置为 UIImageView 时,会抛出一堆 CGContext 错误,并且图像永远不会被设置。然而,在我的设备上
我有一个 UIView,它有 3 个自定义 subview ,其类(目前)称为 LVStemp 并表示树中的节点。 LVStemp 的框架比节点本身大,因为它还包含一些位于节点外部的绘图(尚未编码)。
据我所知,您可以使用两种方法在 CGContext 中绘制渐变,即 drawLinearGradient 和 drawRadialGradient。我正在寻找一种定义椭圆渐变的方法,我可以在其中定义
我正在尝试初始化 CGContext ( API ) let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirector
问题:如何在 CGContext 下显示背景图片? 我使用 Storyboard创建了一个 View Controller ,并放置了一个带有全屏背景图像的 View 。然后,使用相同的 View C
我正在尝试绘制以相等角度分隔的矩形。为此,我首先绘制一个 CGPath,然后对上下文应用转换。我连续做了5次。这是我的代码和输出。如您所见,屏幕截图中的输出不正确,矩形倾斜,不知道为什么。需要知道我做
我正在用 CGContext 渲染一个 NSAttributedString,但是当它渲染文本时是颠倒的 :( 这是我想出的: override func draw(with box: PDFDisp
我编写了一个渲染函数,每 1/60 秒调用一次。如下所示。本质上,它以使用正弦的放射状图案呈现 40 条线。看起来像这样http://imgur.com/a/kPKtT .当我在我的 iPhone 6
我有这段代码可以呈现一个内部带有动态文本的自定义滑动条 handle 。问题是我无法将其缩放到视网膜分辨率。它可以在所有设备上正确绘制并且大小合适,但在视网膜上分辨率较低。如何更改此代码以使最终图像分
当具有不同颜色的路径使用 CGContext 绘制在先前路径的顶部时,如何删除路径? 最佳答案 CGContextBeginPath() 在创建新路径的过程中总是丢弃之前的路径。 关于ios - CG
伙计们! 我需要绘制一些图像到 CGContext。这是相关代码: CGContextSaveGState(UIGraphicsGetCurrentContext()); CGContextRef c
我是一名优秀的程序员,十分优秀!