Draw a UIView and all its subordinates in Context

a UIView is created, and little snow .png is added and removed in the UIView to simulate the effect of snow.

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 a UIView.

But I can only see the image in UIview in the saved jpg. But all added subitems are not saved at this moment.

I would like to know if renderInContext is correct or not to use

+4
source share
1 answer

This is the correct method and it should work.

Note that the method has one big drawback, it does not reflect the actual state of the layer animation (alpha, autoresist, reposition, etc.). If you draw a layer during its animation, this will not work.

+1
source

Source: https://habr.com/ru/post/1337443/


All Articles