What's the difference and compatibility of CGLayer and CALayer?

放肆的年华 提交于 2019-12-17 17:26:00

问题


I'm confusing CGLayer and CALayer. They look similar, so why are there separate implementations? What's the difference between, and compatibility of, CGLayer and CALayer?


回答1:


They are completely different, and not compatible.

To be absolutely clear:

it is strictly a coincidence that the word "layer" is used in both names; they are completely unrelated.

CGLayers are a "special" "high performance" thingy.

You could consider them "like bitmaps, but better."

Apple sat down and said "We're sick of people using bitmaps, let's make something better!" :-)

Indeed, you only work with CGLayers offscreen.

To repeat, CGLayers are entirely used offscreen.

Once they are ready, you can then blast them on to your actual screen (that is to say: blast them on to one of your views).

(However: note that: CGLayers are often used "alone." When I say they are used "alone," I mean they are used for: image processing or mathematical calculations. In that case, CGLayers are never seen onscreen, and they have utterly no connection to the onscreen world.)

In contrast ... CALayers are simply the things "in" views. CALayers are just how views work.

So the two concepts are totally different.

CALayers are just the things "in" views. In contrast CGLayers are Apple's cool "offscreen, high performance, calculation machinery."

To be clear, CGLayers have utterly no connection to views, and no association with views. (Sure, you could paste a CGLayer in to a view, but then, you can paste say "typography" or "an image" in to a view. Typography and images are not views, and CGLayers are not views.)

The typical example is this:

you want to draw something complicated once, and then draw that thing many times on-screen. The answer to that problem is exactly CGLayers. Your "work area" would be a CGLayer; you could then blit it on to a printer, on to the screen, or whatever.

To be clear, in my opinion Apple should have chosen a different name ...

a good name would be "CGCalculationSpace."

The fact that the name contains the word "layer," makes you think of NSLayers.

To repeat: there is absolutely no relationship, whatsoever, in any way. CGLayers are not even vaguely related to layers.

There is no connection, at all. It is really confusing that the letters "l a y e r" are used in the name - it's a shame!!

CGLayers should be called perhaps "workspaces" or "calculation paradigms" or something like that.

Hope it helps and hope you're still reading!



来源:https://stackoverflow.com/questions/4458812/whats-the-difference-and-compatibility-of-cglayer-and-calayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!