iOS”当前图形上下文” -那是什么
当我绘制线条和形状等时,我会在 iOS 中获得“当前图形上下文”。
到底什么是“当前图形上下文”——我正在寻找 30,000 英尺的描述。
现在我只是复制并粘贴 UI 代码,不太确定它在做什么。
When I draw lines and shapes etc I get the " current graphics context" in iOS.
What exactly though is " current graphics context" - I'm looking for the 30,000 foot description.
Right now I just copy and paste UI code, not exactly sure what it's doing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
图形上下文是存储有关绘图状态的信息的地方。这包括填充颜色、描边颜色、线宽、线条图案、缠绕规则、蒙版、当前路径、透明层、变换、文本变换等。使用 CoreGraphics 调用时,您可以指定每个函数要使用的上下文。这意味着您可以同时使用多个上下文,但通常只使用一个。在 UIKit 层,有“当前”图形上下文的概念,它是所有 UIKit 级绘图调用(例如
-[UIColor set]
或UIBezierPath
绘图)。当前上下文存储在上下文堆栈中,因此您可以为某些绘图创建新的上下文,然后当您完成它时,将恢复以前的上下文。通常,您免费获取上下文-[ UIView drawRect:]
位于 CALayer 显示相关方法内,但其他情况除外。过去,“当前”上下文是应用程序范围的全局状态,因此在主线程之外进行触摸是不安全的。从 iOS 4.0(我相信)开始,这变成了线程本地状态,并且 UIKit 级别的绘图方法可以在后台线程上安全使用。
A graphics context is the place where information about the drawing state is stored. This includes fill color, stroke color, line width, line pattern, winding rule, mask, current path, transparency layers, transform, text transform, etc. When using CoreGraphics calls, you specify the context to use to every single function. This means you can use multiple contexts at once, though typically you only use one. At the UIKit layer, there is the concept of a "current" graphics context, which is a graphics context that's used by all UIKit-level drawing calls (such as
-[UIColor set]
orUIBezierPath
drawing). The current context is stored in a stack of contexts, so you can create a new context for some drawing, then when you finish with it the previous context is restored. Typically you get a context for free inside of-[UIView drawRect:]
inside of CALayer display-related methods, but not otherwise.It used to be that the "current" context was an application-wide global state, and therefore was not safe to touch outside of the main thread. As of iOS 4.0 (I believe), this became a thread-local state and UIKit-level drawing methods became safe to use on background threads.
操作系统需要一个地方来保存信息,例如绘图状态,您不希望在每个 CG 绘图命令中指定这些信息,例如要在哪个位图或视图中绘制、要使用的比例或其他变换、最后的颜色上下文
告诉每个 CG 调用在哪里可以找到当前绘图调用的所有这些“东西”。为完全相同的绘图调用提供不同的上下文,该调用可能会在完全不同的视图中绘制到不同的位图,具有不同的颜色、不同的比例等。
The OS needs a place to save information, such as drawing state, which you don't want to specify in every single CG drawing command, such as in which bitmap or view to draw, the scale or other transform to use, the last color you specified, etc.
The context tells each CG call where to find all this "stuff" for your current drawing call. Give a different context to the exact same drawing call, and that call might draw to a different bitmap in a completely different view, with a different color, different scale, etc.
基本上,它是平台(iOS、Android、JavaME 等)中的一个类,提供对该平台提供的所有绘图/显示功能的访问。当然,对于不同的平台,它会有所不同,但这是 30,000 英尺的描述:)
Basically it is a class in a platform (iOS, Android, JavaME and many others) that provides access to all the drawing/display capabilities provided for that platform. It varies a bit for different platforms of course, but this is the 30,000 foot description :)