ios - 什么时候需要设置 CALayer 的 contentsScale 属性?

标签 ios objective-c macos calayer

documentation Apple 声明在以下情况下您需要考虑层的比例因子:

  • Creates additional Core Animation layers with different scale factors and composites them into its own content
  • Sets the contents property of a Core Animation layer directly

最后一句话我不是很清楚。 他们还说:

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.

通常还是总是?这仅对 View 的托管支持层或我作为子层添加到支持层的每一层有效?因为他们也说:

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.


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

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。
当我的层调用委托(delegate)方法 drawLayer:inContext: 绘制内容并且该层不是 View 的支持层时,我是否还需要设置它?

最佳答案

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

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(context, 2, 2)。

这样您就可以在 10x10 的概念空间中以任何一种方式进行绘图,但如果像素加倍,则后备存储也会加倍。

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)?

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

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

是的。但是文档中的这段话告诉你的是,如果你不想看起来丑陋,contentsScale 为 2 的 10pt x 10pt 图层需要 20px x 20px 图像的内容。 IE。如果要避免缩放伪影,则在直接设置图层内容时需要注意图层的 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?

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

关于ios - 什么时候需要设置 CALayer 的 contentsScale 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18459078/

相关文章:

ios - 如何获取 `placemark` 属性?

ios - Xcode Assets 是否需要不同的图像大小?

ios - 为什么当我将 int 转换为 NSInteger 时值会发生变化?

objective-c - 对 UITextField 上的操作进行单元测试

ios - iOS系统下如何获取objective-c中的所有进程信息?

sql-server - 为什么 macOS 上的 .NET Core 项目无法连接到 SQL Server Express Docker 容器?

android - 使用 ARM 的 newlib 交叉编译 GCC : how to specify GCC options like -march?

ios - UNIRest:在异步 HTTP 响应上切换到新的 ViewController

ios - 无法在 iOS Appium 测试 (AWS Device Farm) 上切换到 WebView 上下文

objective-c - 访问 Controller 类范围之外的对象