在iOS上,在我们从上下文创建图层并获取图层的上下文之后,这些上下文如何相互关联? [英] On iOS, after we create a layer from context and get the layer's context, how do these contexts relate to each other?

查看:146
本文介绍了在iOS上,在我们从上下文创建图层并获取图层的上下文之后,这些上下文如何相互关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以从当前图形上下文创建一个图层,然后获取图层的上下文:

We can create a layer from the current graphics context and then get the layer's context:

CGContextRef context = UIGraphicsGetCurrentContext();

CGLayerRef layer = CGLayerCreateWithContext(context, 
                                            CGSizeMake(self.frame.size.width,
                                                self.frame.size.height), NULL);

CGContextRef contextOfLayer = CGLayerGetContext(layer);

所以我们现在有2个上下文: context contextOfLayer 。这两种情境如何相互关联?是 contextOfLayer 实际上是 context context 的一部分有一个数组层上下文指针?如果我使用 NSLog(@%p,...)打印出地址,则它们具有不同的地址,因此它们不是同一个对象。而且我认为 contextOfLayer 不会影响上下文堆栈,所以它只是一个独立的上下文,它本身就是存在吗?

So we now have 2 contexts: context and contextOfLayer. How do these two contexts relate to each other? Is contextOfLayer actually part of context and context has a array of layer context pointers? If I print out their addresses using NSLog(@"%p", ...), they have different addresses, so they are not the same object. And I think contextOfLayer doesn't affect the context stack, so is it just an independent context just kind of "exist out there" by itself?

推荐答案

如果您了解 CGLayer 的基本原理,这会有所帮助。 CGLayer 在重复将相同内容绘制到特定类型的上下文中时,将作为优化。

It helps if you understand the rationale for CGLayer. CGLayer is meant as an optimization when you repeatedly draw the same content into a particular kind of context.

(例如:具有特定格式的位图上下文 - 例如32位RGBA - 或PDF上下文等)

(For instance: bitmap contexts with a particular format -- e.g. 32-bit RGBA -- or PDF contexts, etc.)

当你创建 CGLayer 时,你传递它想要绘制 CGLayer 的上下文成。这让 CGLayer 为这种上下文优化自己。

When you create the CGLayer, you pass it the context that you intend to draw the CGLayer into. This lets the CGLayer optimize itself for that kind of context.

稍后,如果你绘制 CGLayer 进入该上下文,或进入具有相同格式的不同上下文,绘图将更快。

Later on, if you draw the CGLayer into that context, or into a different context that has the same format, the drawing will be faster.

如果你画a CGLayer 进入具有不同格式的上下文,绘图仍然有效,并且您不会收到错误。但是,它可能不会比你刚刚直接进入上下文时更快。

If you draw a CGLayer into a context with a different format, the drawing will still work, and you will not get an error. However, it may not be any faster than it would have been if you had just drawn directly into that context.

The documentation for CGLayerCreateWithContext says:


context

图形上下文你想要创建相对于的图层。
图层使用此图形上下文作为
初始化的参考。

The graphics context you want to create the layer relative to. The layer uses this graphics context as a reference for initialization.

这意味着该图层显示给定的上下文作为参考。它并不一定意味着 context 存储在 CGLayer 中,或永久引用。它可能是,但你不能说 - 它是一个内部实现细节。

This means that the layer looks at the given context as a reference. It does not necessarily mean that the context is stored within the CGLayer, or permanently referenced. It might be, but you can't tell -- it's an internal implementation detail.

CGLayerGetContext的文档


返回的上下文是图层本身的上下文,而不是您在创建图层时指定的上下文。

The context that’s returned is the context for the layer itself, not the context that you specified when you created the layer.

所以你应该期望 context!= contextOfLayer

同样,API没有指定关于这些上下文如何相互关联的任何内容 - 它们可能在内部相互引用,或者它们可能不相互引用。作为API的用户,我们不应该假设任何内容。

Again, the API does not specify anything about how those contexts relate to each other -- they might reference each other internally, or they might not. As users of the API we should not assume anything.

具体回答您的问题:


contextOfLayer实际上是上下文的一部分,而context有一个层上下文指针数组?

Is contextOfLayer actually part of context and context has a array of layer context pointers?

我们不知道,也无法找到答案。作为API的用户,我们不应该编写任何采用这种或那种方式的代码。

We don't know and we can't find out. As users of the API we shouldn't write any code that assumes one way or another.


我认为contextOfLayer不会影响上下文堆栈,它只是一个独立的上下文,它本身就是存在吗?

I think contextOfLayer doesn't affect the context stack, so is it just an independent context just kind of "exist out there" by itself?

是的,它是一个独立的上下文。 UIKit中的上下文堆栈是一个更高级别的概念。 CoreGraphics中的 CGLayer 是一个较低级别的API,对 UIKit 一无所知。

Yes, it is an independent context. The "context stack" in UIKit is a higher-level concept. CGLayer in CoreGraphics is a lower-level API that doesn't know anything about UIKit.

这篇关于在iOS上,在我们从上下文创建图层并获取图层的上下文之后,这些上下文如何相互关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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