Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between CALayer -drawInContext: and -renderInContext:?

What's the difference between CALayer -drawInContext: and -renderInContext: ?

like image 392
dontWatchMyProfile Avatar asked Mar 30 '10 08:03

dontWatchMyProfile


People also ask

What is the difference between UIView and CALayer?

Working with UIViews happens on the main thread, it means it is using CPU power. CALayer: Layers on other hand have simpler hierarchy. That means they are faster to resolve and quicker to draw on the screen. There is no responder chain overhead unlike with views.

What is CALayer?

An object that manages image-based content and allows you to perform animations on that content.

What is the layer property on a UIView object?

All UIView subclasses have a layer property, which is responsible for drawing their contents efficiently. These layers are powered by Core Animation, which handles all the drawing and animation that UIKit requests.

What is layer in view IOS?

the main different between a view and a layer is that views can accept user input while a layer cannot. a layer is simply a graphical representation. views can handle user taps, drags, pinches, etc. to me, this is all you need to know.


1 Answers

When providing custom Quartz-drawn content to display within a CALayer, you can override -drawInContext: and do your custom drawing there. This is similar to -drawRect: for a UIView or NSView. Alternatively, you can set another class to be the delegate of the CALayer and implement -drawLayer:inContext: to provide custom content to a standard CALayer.

You don't override -renderInContext:, but instead you can call this on a layer to render it and all of its sublayers into a particular Core Graphics context. Note that this won't render certain types of layers (like those with OpenGL content). It also doesn't behave the way you'd expect when rendering into a PDF context, where the layers will come out as bitmapped rectangles instead of pure vector elements. To work around this, you might want to check out the Core Plot framework's CPTLayer implementation, where we bypass the normal rendering process in order to preserve the vectors in a PDF generated from our CALayer subclass.

like image 91
Brad Larson Avatar answered Sep 20 '22 08:09

Brad Larson