Search code examples
ios3dcore-animationrendercatransformlayer

Render CATransformLayer to PNG in 3D


I'm trying to render a CATransformLayer that contains several CAShapeLayers to a png file. I know how to set up the basic rendering and that does work.

Though when I call renderInContext on the CATransformLayer, it flattens everything to 2D and completely ignores my transformation matrix (rotation and / or perspective).

What can I do to render my 3d CATransformLayer with all its CAShapeLayers to PNG / UIImage?


Solution

  • Since you're targeting only iOS7 and above, I would suggest you ditch renderInContext: and replace it with -[UIView drawViewHierarchyInRect:afterScreenUpdates:]. Your code will probably look something like this then:

    UIGraphicsBeginImageContext(sizeOfYourImage);
    
    [yourViewToRender drawViewHierarchyInRect:(CGRect){0, 0, sizeOfYourImage} afterScreenUpdates:YES];
    
    UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();