所以CALayer不包含视图的内容位图? [英] So a CALayer does not contain a content bitmap of a view?

查看:114
本文介绍了所以CALayer不包含视图的内容位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从文档:


图层是轻量级对象
(CALayer),尽管类似于
视图,实际上是分配给视图的模型对象


轻量级为我排除了任何重要的位图。我认为CALayer是真正的事情,而UIView只是它的包装。每个视图有3个CALayers在不同的树(模型,演示文稿,渲染)。所以没有3位图?只有一个?

解决方案

CALayer中的轻量级一词来源于Mac上的文档。正如Joe指出的,与iPhone的UIView相比,NSView是一个相当复杂的UI元素。你甚至可以在资源有限的移动设备上在屏幕上动画化几十个UIView,但是当你开始将许多UIView添加到屏幕上时,NSView会给系统带来更大的压力。这是UIKit在AppKit上的新开始所获得的事情之一,因为UIKit从一开始就拥有Core Animation,而Apple有机会从AppKit中学到什么工作,什么没有。



相比之下,CALayer对其绘制的基于GPU的位图矩形纹理增加很少,因此它们不会增加很多开销。在iPhone上,这与UIView没有太大的不同,因为UIView只是一个CALayer的轻量级包装。



我不同意Count Chocula对此,并说,一个CALayer似乎包装一个位图纹理在GPU上。是的,您可以指定自定义Quartz图形来构成图层的内容,但该图形只在必要时发生。一旦绘制了图层中的内容,就不需要为要移动或以其他方式动画的图层重绘。如果对图层应用变换,则会在放大时看到像素化,这表示它不处理矢量图形。



此外,核心绘图框架(以及在我自己的应用程序中),我们不得不重写CALayers的正常绘制过程,因为正常的 -renderInContext:方法对于PDF不能很好地工作。如果使用它将图层及其子图层渲染​​为PDF,则会发现这些图层是由最终PDF中的光栅位图表示的,而不是它们应该具有的矢量元素。只有通过使用不同的渲染路径,我们才能为我们的PDF获得正确的输出。



我还没有使用新的 shouldRasterize



rasterizationScale p>实际上,你会发现,CALayers(和UIViews与它们的支持层)确实消耗了大量的内存,当你考虑他们的位图内容。 轻量级度量是它们在内容上添加多少,这是非常少的。您可能无法从像对象分配这样的工具中看到内存使用情况,但是当您向应用程序添加大层时,可以查看内存监视器,并且您会在应用程序或SpringBoard(拥有Core Animation服务器)中看到内存尖峰。 / p>

当涉及到表示层和模型时,位图不会在它们之间重复。应该只有在给定时刻屏幕上显示的一个位图纹理。不同的层仅仅跟踪在任何给定时刻的属性和动画,因此在每个中存储很少的信息。


From the docs:

"Layers are lightweight objects (CALayer) that, though similar to views, are actually model objects assigned to views."

Lightweight for me excludes any heavy bitmap for content. I believed a CALayer is the "real" thing, while a UIView is just the wrapper around it. Every view has 3 CALayers in different trees (model, presentation, render). So no 3 bitmaps? Only one?

解决方案

The term "lightweight" in reference to a CALayer comes from that piece of documentation originating on the Mac. As Joe points out, an NSView is a fairly complex UI element when compared to the iPhone's UIView. You can animate dozens of UIViews around the screen on even a resource-constrained mobile device, but NSViews put a lot more strain on the system as you start adding many of them to the screen. This is one of the things gained by the fresh start of UIKit over AppKit, because UIKit has had Core Animation from the beginning, and Apple had a chance to learn from what worked and what didn't in AppKit.

In comparison, a CALayer adds very little to the underlying GPU-based bitmapped rectangular texture that it is drawing, so they don't add a lot of overhead. On an iPhone, this isn't very different from a UIView, because a UIView is then just a lightweight wrapper around a CALayer.

I'm going to disagree with Count Chocula on this, and say that a CALayer does appear to wrap a bitmapped texture on the GPU. Yes, you can specify custom Quartz drawing to make up the layer's content, but that drawing only takes place when necessary. Once the content in a layer is drawn, it does not need to be redrawn for the layer to be moved or otherwise animated around. If you apply a transform to a layer, you'll see it get pixelated as you zoom in, a sign that it is not dealing with vector graphics.

Additionally, with the Core Plot framework (and in my own applications), we had to override the normal drawing process of CALayers because the normal -renderInContext: approach did not work well for PDFs. If you use this to render a layer and its sublayers into a PDF, you'll find that the layers are represented by raster bitmaps in the final PDF, not the vector elements they should be. Only by using a different rendering path were we able to get the right output for our PDFs.

I've yet to play with the new shouldRasterize and rasterizationScale properties in iOS 3.2 to see if they change the way this is handled.

In fact, you'll find that CALayers (and UIViews with their backing layers) do consume a lot of memory when you take their bitmapped contents into account. The "lightweight" measure is how much they add on top of the contents, which is very little. You might not see the memory usage from an instrument like Object Allocations, but look at Memory Monitor when you add large layers to your application and you'll see memory spikes in either your application or SpringBoard (which owns the Core Animation server).

When it comes to the presentation layer vs. the model one, the bitmap is not duplicated between them. There should only be the one bitmapped texture being displayed to the screen at a given moment. The different layers merely track the properties and animations at any given moment, so very little information is stored in each.

这篇关于所以CALayer不包含视图的内容位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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