简体   繁体   English

来自CALayer的CGImage(或UIImage)

[英]CGImage (or UIImage) from a CALayer

I have created a CALayer (I added several shapes to the layer, but ultimately, I have a single CALayer), and I am having a LOT of trouble adding it to a CGImage. 我创建了一个CALayer (我在图层中添加了几个形状,但最终,我有一个CALayer),而且我很难将它添加到CGImage中。 I have found some references to [CALayer renderInContext:ctx] , but I am not sure how to implement it. 我找到了一些[CALayer renderInContext:ctx]引用,但我不知道如何实现它。

Has anyone done this before? 有没有人这样做过?

Try this... 试试这个...

UIGraphicsBeginImageContext(layer.bounds.size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Create a bitmap graphics context and use renderInContext: to draw the layer into it. 创建位图图形上下文并使用renderInContext:将图层绘制到其中。 You now have the layer in an image. 您现在拥有图像中的图层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM