Reputation: 13807
I'm in the process of trying to tweak the process of a game which uses Quartz to draw a moderate number of sprites, around 50, during each game loop (40 fps).
At the moment I have a very simple drawing mechanism where a single game view (UIView) iterates over all the active sprites and asks each one to render itself to a CGContext (that of the view). This works fine, but performance is starting to drop with more than 50 active objects and I'd quite like to tweak things.
I've decided that holding a CALayer for each sprite which its cached drawing is the way to go - then using Core Animation to render/rotate/scale the drawing.
I'm struggling to understand how I achieve this. Where exactly do I initially draw to? I don't have a CGContext when my sprite is initialised. Is the right approach to render to a CGImage as a buffer then set it as the content on the CALayer?
Upvotes: 1
Views: 1214
Reputation: 135540
Just subclass CALayer
and do your drawing in drawInContext:
. The layer will then cache its contents automatically. If you don't want to subclass CALayer
, you can also assign it a delegate (which must not be a UIView
) and implement drawLayer:inContext:
.
Upvotes: 1