如何使用CALayer renderInContext并保持透视变换?

jjx*_*tra 6 core-graphics calayer ios

如何使用CALayer renderInContext(m34)保留图层的透视变换?看起来这个方法假设一个Identity变换.我已经尝试过改变上下文这样的事情,这对于平移,旋转和缩放来说很好,但似乎没有办法保持3D变换的视角.

也许是提出这个问题的更好方法:如何在绘制CGContextRef时应用透视图?

小智 5

您可能想要查看drawViewHierarchyInRect:afterScreenUpdates:自iOS 7.0以来提供的方法.将该方法应用于包含CALayers的视图将保持其3D变换.

示例代码:

BOOL opaqueBackground = YES;
UIGraphicsBeginImageContextWithOptions(theViewContainingYourCALayers.bounds.size, !opaqueBackground, 0);
[theViewContainingYourCALayers drawViewHierarchyInRect:theViewContainingYourCALayers.bounds afterScreenUpdates:YES];
[UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

正如马特指出的那样,完全依靠renderInContext:是不可能的,因为它无视除了仿射之外的任何转变.

迟到的答案,但我希望它仍然有帮助.