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

eonil picture eonil · Dec 16, 2010 · Viewed 9.3k times · Source

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?

Answer

Fattie picture Fattie · Jan 4, 2011

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!