CGImage (or UIImage) from a CALayer

2019-03-20 18:16发布

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. I have found some references to [CALayer renderInContext:ctx], but I am not sure how to implement it.

Has anyone done this before?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-03-20 18:43

Create a bitmap graphics context and use renderInContext: to draw the layer into it. You now have the layer in an image.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-20 18:45

Try this...

UIGraphicsBeginImageContext(layer.bounds.size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
查看更多
登录 后发表回答