简体   繁体   English

将UIView及其所有子视图绘制到Context

[英]Draw UIView and all its subview to Context

a UIView is created and small snow.png is kept added and removed to UIView to simulate the snowing effect. 创建一个UIView并保持小snow.png添加并移除到UIView以模拟下雪效果。

Then I would like to capture the screen as an image. 然后我想将屏幕捕获为图像。

UIGraphicsBeginImageContext(self.uiviewPreview.bounds.size);
[self.uiviewPreview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

Note: self.uiviewPreview is the UIView. 注意:self.uiviewPreview是UIView。

But I can only see the image in UIview in the saved jpg. 但我只能在保存的jpg中看到UIview中的图像。 But all added subviews at that moment are not saved. 但是当时所有添加的子视图都没有保存。

I would like to know whether renderInContext is correct or not to use 我想知道renderInContext是否正确使用

It's the right method and it should work. 这是正确的方法,它应该工作。

Note that the method has one big flaw, it doesn't reflect the actual state of layer animations (alpha, autoresizing, repositioning etc.). 请注意,该方法有一个很大的缺陷,它不反映图层动画的实际状态(alpha,autoresizing,repositioning等)。 If you are drawing the layer while animating it, it won't work. 如果您在动画时绘制图层,它将无法正常工作。

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

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