我什么时候需要设置CALayer的contentsScale属性? [英] When do I need to set the contentsScale property of a CALayer?

查看:147
本文介绍了我什么时候需要设置CALayer的contentsScale属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档 Apple声明你需要考虑图层的比例因子:

In the documentation Apple states that you need to consider the scale factor of a layer when it:



  • 创建具有不同比例因子的其他Core Animation图层并将它们合成为自己的内容

  • 直接设置Core Animation图层的contents属性

最后一句话对我来说并不完全清楚。
他们还说:

The last statement is not totally clear to me. They also say:


直接使用Core Animation图层提供
内容的应用程序可能需要调整他们的绘图代码以计算规模
因子。通常,当您在视图的drawRect:方法中绘制,或者在图层委托的drawLayer:inContext:方法中绘制
时,系统
会自动调整图形上下文以考虑规模
因子。

Applications that use Core Animation layers directly to provide content may need to adjust their drawing code to account for scale factors. Normally, when you draw in your view’s drawRect: method, or in the drawLayer:inContext: method of the layer’s delegate, the system automatically adjusts the graphics context to account for scale factors.

Normallly还是一直?这仅适用于视图的托管后备层或我作为后备层子层添加的每个层?因为他们还说:

Normallly or always? and this is valid only for view's hosted backing layer or every layer I add as a sublayer to the backing layer? because they also say:


如果您的应用程序创建没有关联视图的图层,则每个
新图层对象的比例因子最初设置为1.0。如果您没有
更改该比例因子,并且如果您随后在
高分辨率屏幕上绘制图层,则图层的内容会自动缩放
以补偿比例因子的差异。如果您不想要
要缩放的内容,您可以将图层的比例因子更改为
2.0,但如果您这样做而没有提供高分辨率内容,那么您现有的内容可能会显得比您更小期待。要修复
这个问题,你需要为
图层提供更高分辨率的内容。

If your application creates layers without an associated view, each new layer object’s scale factor is set to 1.0 initially. If you do not change that scale factor, and if you subsequently draw the layer on a high-resolution screen, the layer’s contents are scaled automatically to compensate for the difference in scale factors. If you do not want the contents to be scaled, you can change the layer’s scale factor to 2.0, but if you do so without providing high-resolution content, your existing content may appear smaller than you were expecting. To fix that problem, you need to provide higher-resolution content for your layer.



我在想象一个没有视图的图层时遇到困难,我可以想象一下图层的层次结构但是一旦我想要显示它们,我应该将它添加为视图背衬层的子层次,或者确实存在一些使用它们的其他方法(比如在上下文中创建和渲染)?


I'm having difficulties imagining a layer without a view, I can imagine a hierarchy of layers but as soon as I want to display them I should add it as a subhierachy of the view's backing layer, or do exist some other means to use them (like creating and just renderring in a context)?


Core Animation的合成引擎查看contentsScale
每个图层的属性,用于确定在合成期间是否需要缩放该图层
的内容。如果您的应用程序创建没有关联视图的
图层,则每个新图层对象的比例
因子最初设置为1.0。如果您没有更改该比例
因子,并且随后在高分辨率
屏幕上绘制图层,则图层的内容会自动缩放以补偿
的比例因子差异。如果您不希望缩放内容
,则可以将图层的比例因子更改为2.0,但如果您在不提供高分辨率内容的情况下执行
,则可能会显示现有的
内容小于你的预期。要修复
问题,您需要为图层提供更高分辨率的内容。

Core Animation’s compositing engine looks at the contentsScale property of each layer to determine whether the contents of that layer need to be scaled during compositing. If your application creates layers without an associated view, each new layer object’s scale factor is set to 1.0 initially. If you do not change that scale factor, and if you subsequently draw the layer on a high-resolution screen, the layer’s contents are scaled automatically to compensate for the difference in scale factors. If you do not want the contents to be scaled, you can change the layer’s scale factor to 2.0, but if you do so without providing high-resolution content, your existing content may appear smaller than you were expecting. To fix that problem, you need to provide higher-resolution content for your layer.

这实际意味着什么?当我做那样的事情 layer.contents =(id)image.CGImage 我需要设置contentsScale。

当我的图层绘制它的内容时,我还需要设置它,调用委托方法 drawLayer:inContext:并且图层不是支持图层一个视图?

What does that actually mean? When I do something like that layer.contents = (id)image.CGImage I need to set the contentsScale.
Do I also need to set it when my layer draws it's contents calling the delegate method drawLayer:inContext: and the layer isn't the backing layer of a view?

推荐答案

re:图层内容通过-drawRect:或-drawLayer:inContext:

re: layer content via -drawRect: or -drawLayer:inContext:


Normallly或always?这仅适用于视图的托管后备层或每个层
我作为子层添加到后备层?

Normallly or always? and this is valid only for view's hosted backing layer or every layer I add as a sublayer to the backing layer?

始终。调用这些方法时,当前上下文已经应用了缩放因子。例如,如果您的图层是10pt x 10pt,则它的边界为{0,0,10,10},与contentsScale无关。如果contentsScale = 2(例如你在视网膜上),这实际上是20x20像素。 CALayer已经设置了20x20px后备存储并应用了比例因子CGContextScaleCTM(上下文,2,2)。

Always. When these methods are called, the current context will already have the scaling factor applied. For instance, if your layer is 10pt x 10pt, it has a bounds of {0,0,10,10}, regardless of contentsScale. If the contentsScale = 2 (e.g. you are on retina), this is actually 20x20 pixels. CALayer will have already setup a 20x20px backing store and applied a scale factor CGContextScaleCTM(context, 2, 2).

这样您就可以在10x10中进行绘图概念空间,无论哪种方式,但如果像素加倍,后备存储加倍。

This is so that you can do your drawing in your 10x10 conceptual space either way, but the backing store is doubled if the pixels are doubled.


我很难想象没有视图的图层,我可以想象
图层的层次结构,但是一旦我想要显示它们,我应该将它添加为
视图的后备层的子层次,或者确实存在一些其他方法来使用它们(比如创建它们)只需
在上下文中渲染)?

I'm having difficulties imagining a layer without a view, I can imagine a hierarchy of layers but as soon as I want to display them I should add it as a subhierachy of the view's backing layer, or do exist some other means to use them (like creating and just renderring in a context)?

您可以创建它们,将它们添加为子图层,然后设置它们的委托到您实现-drawLayer:inContext:的对象,或者您可以直接设置图层的内容。在iOS上,UIView是一个相当浅的CALayer包装器,因此创建子视图通常是有意义的,而不是直接创建子层,这就是为什么你不熟悉这种用法。在OS X上,支持图层的NSView是一个围绕CALayer的大而复杂的包装器,因此在单个视图中制作整个层次结构更有意义。

You can create them, add them as sublayers, and either set their delegate to an object where you implement -drawLayer:inContext:, or you can directly set the layer's contents. On iOS, UIView is a fairly shallow wrapper around a CALayer so it generally makes sense to create subviews instead of directly making sublayers, which is why you aren't so familiar with this usage. On OS X, a layer-backed NSView is a big and complicated wrapper around a CALayer, so it makes a lot more sense to make whole hierarchies of layers within a single view.

这究竟意味着什么?当我做那样的事情时,这个层.contents =
(id)image.CGImage我需要设置contentsScale。

What does that actually mean? When I do something like that layer.contents = (id)image.CGImage I need to set the contentsScale.

是的。但是文档中的这段经文告诉你的是,如果你想看起来不丑,10pt x 10pt层的contentsScale为2,则需要20px x 20px图像的内容。即如果要避免缩放工件,则需要在直接设置图层内容时注意图层的contentsScale。

Yes. But what this passage in the documentation is telling you is that if you want to not look ugly, a 10pt x 10pt layer with contentsScale of 2, needs contents that are a 20px x 20px image. I.e. You need to pay attention to the contentsScale of the layer when you are directly setting the layer contents if you want to avoid scaling artifacts.


我还需要在我的图层绘制内容时调用委托方法
drawLayer:inContext:并且图层不是视图的后备层吗?

Do I also need to set it when my layer draws it's contents calling the delegate method drawLayer:inContext: and the layer isn't the backing layer of a view?

是的。如果您自己创建图层(不是通过UIView创建),则需要手动设置其比例。通常这只是layer.contentsScale = [[UIScreen mainScreen] scale]。 (同样,在OS X上,这有点复杂,因为屏幕可以在运行时进出。)

Yes. If you are creating layers yourself (not created via a UIView), you need to set their scale manually. Usually this is just layer.contentsScale = [[UIScreen mainScreen] scale]. (Again, on OS X this is a little more complicated since screens can come and go while running.)

这篇关于我什么时候需要设置CALayer的contentsScale属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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