iPhone - 试图理解 CGContext 中的 UIBezier
当我有一个 UIBezier 并使用 drawRect 将它添加到视图上时,我发现贝塞尔曲线是一种不稳定的,我的意思是,它实际上没有在任何地方绘制。相反,它就像是写在一种与我在该视图上看到的内容相对应的缓冲区中。我的意思是,如果我使用 [self setNeedsDisplay] 再次调用drawRect,贝塞尔曲线就会消失,我可以编写其他内容。
另一方面,如果我在 CGContext 上画一个像,
CGContextStrokePath(ctx);
我实际上是在将该行写入该上下文,并且无法删除它,如果我再次调用相同的方法,它将覆盖现在已经有一个的上下文绘制的线。
但是当我使用 UIBezier 命令在 CGContext 上写入时会发生什么?
我的意思是,如果我做类似的事情
UIGraphicsPushContext(ctx);
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0, height);
CGContextScaleCTM(ctx, 1.0, -1.0);
[myBezier stroke];
CGContextRestoreGState(ctx);
UIGraphicsPopContext();
,这一行是永久写入 ctx 还是像以前一样不稳定?这些文档不包含任何有关它的信息,并且一如既往地含糊不清。
谢谢
When I have a UIBezier and I stoke it on a view using drawRect I see the bezier is a a kind of volatile, I mean, it is really not drawn anywhere. Instead it is like it was written in a kind of buffer that corresponds to what I see on that view. I mean, if I invoke drawRect again using [self setNeedsDisplay] the bezier is gone and I can write other stuff.
On the other hand, if I draw a like on a CGContext using
CGContextStrokePath(ctx);
I am really writing the line to that context and there's no way to erase it and if I call the same method again, it will write over the context that now has already one line drawn.
But what happens when I use a UIBezier command to write on a CGContext?
I mean, if I do something like
UIGraphicsPushContext(ctx);
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0, height);
CGContextScaleCTM(ctx, 1.0, -1.0);
[myBezier stroke];
CGContextRestoreGState(ctx);
UIGraphicsPopContext();
is this line permanently written to ctx or is it volatile as before? The docs contain no information about it and are vague as always.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当你的视图的
drawRect:
被调用时,UIKit 就会设置一个图形上下文供你绘制。这就是为什么贝塞尔曲线路径看起来不是永久的;每次需要显示视图时,都会有一个全新的上下文,并且您的路径会被吸引到其中。因此,每次通过drawRect:
,您都可以决定不绘制特定路径,并且不会显示该路径。一旦将路径添加到上下文中,就无法将其删除,但下次刷新视图时,会出现一个新的上下文,其中几乎没有任何内容。如果您创建自己的图形上下文,那么您放入其中的任何内容都是“永久的”——它将在上下文的生命周期内一直存在。
上下文基本上由将生成“图片”的绘图指令组成。这是模糊的,因为上下文的目的地(将绘制图片的位置)可以是不同的东西:图像文件、设备屏幕的一部分,甚至可能是一张纸。上下文一旦充满绘图指令,就会被渲染到目的地。
在视图绘制的情况下,UIKit 会为您设置上下文并调用您的
drawRect:
。这里存在一定程度的间接性——您永远不会真正“绘制到视图中”,总是进入上下文。您的视图本质上保留并代表屏幕的一部分。 UIKit 会询问您想要在该部分中放入什么内容,并为您提供图形上下文,以便您可以传达该信息。然后它获取充满指令的上下文,将其转换为像素数据,并将其绘制在视图表示的区域中。Whenever your view's
drawRect:
gets called, UIKit has set up a graphics context for you to draw into. That's why it appears that the bezier paths aren't permanent; every time the view needs to be displayed there's a fresh new context, and your paths get drawn into that. Each time throughdrawRect:
, therefore, you can decide not to draw a particular path and it won't be displayed.There's no way to remove a path once it's been added to the context, but the next time the view is refreshed, there's a new context with almost nothing in it. If you create your own graphics context, whatever you put into it is "permanent" -- it will be there for the lifetime of the context.
The context basically consists of drawing instructions that will produce a "picture". This is vague because the destination for a context -- where the picture will be drawn -- can be different things: an image file, a section of the device screen, possibly even a piece of paper. The context, once it's full of drawing instructions, then gets rendered to its destination.
In the case of your view drawing, UIKit sets up that context for you and calls your
drawRect:
. There's a certain amount of indirection here -- you're never really "drawing into a view", always into a context. Your view essentially reserves and represents a section of the screen. UIKit asks you what you would like to put into that section, and gives you the graphics context so that you can convey that information. Then it takes the context, which is full of instructions, turns it into pixel data, and paints that in the area represented by your view.UIBezierPath
是一个包含一系列直线/曲线以及如何绘制它们的说明的类。当您绘制贝塞尔曲线路径时,它本质上与 Quartz 中的任何其他绘图函数执行相同的操作。上下文的内容将在上下文的生命周期内持续存在(对于位图上下文,这就是位图在内存中的生命周期;对于视图上下文,这就是“直到需要重新绘制视图”。)UIBezierPath
is a class that encompasses a series of lines/curves and instructions on how to draw them. When you draw a bezier path, it essentially does the same thing as any other drawing function in Quartz. The content of the context will persist for the lifetime of the context (for a bitmap context, that's the lifetime of the bitmap in memory; for a view context, that's "until the view needs to be redrawn.")