Search code examples
core-animationcalayer

One CGImageRef for multiple CALayers


I have been experimenting a lot lately with different technologies for drawing 2D-sprites on iOS (CoreAnimation, UIViews, CGContext and Open GL ES). The best performance versus developmenttime I got from CoreAnimation so I will stick to that (for the time being).

To display bitmaps I use CALayers - which works just fine and not much slower than Open GL. There are multiple patterns which repeat often (which is typical for a platforming game). For memorysaving reasons I would like to reuse a stored Image for multiple CALayers.

Can I do that? How? I imagine something like:

  1. Store Bitmap in whatever form (UIImage?) in memory
  2. Get a CGImageRef from UIImage
  3. Apply CGImageRef to each CALayer which should display that Bitmap

Will this actually store only the pointer to the bitmap for each Layer or does "Apple-magic" create a copy of the bitmap for each CALayer?

Thank you.


Solution

  • After creating an iOS project to test this, here is my conclusion:

    • you can set the content (type id) to UIImage. period. So imho being type id is useless: it should be UIImage
    • hence: you cannot set content to type CGImageRef (if you do with __bridge id) the content is nil and no error is generated (in contrast to Apple Documentation, which says this works on MacOS X 10.6 and higher which iOS is based on afaik).

    I found no way of displaying a tile multiple times without using RAM for each tilebitmap working with CALayer. You have to work with OpenGL ES to do that.