需要将UIView捕获到UIImage中,包括所有子视图 [英] Need to capture UIView into a UIImage, including all subviews
问题描述
我需要将UIView及其所有子视图捕获到UIImage中。问题是视图的一部分是在屏幕外,所以我不能使用屏幕捕获功能,当我尝试使用UIGraphicsGetImageFromCurrentImageContext()函数时,它似乎也没有捕获子视图。它应该捕获子视图,我只是做错了什么?如果没有,还有其他方法可以实现吗?
I need to capture a UIView and all it's subviews into a UIImage. The problem is that part of the view is off screen, so I can't use the screen capture function, and when I try to use the UIGraphicsGetImageFromCurrentImageContext() function, it doesn't seem to capture the subviews as well. Should it be capturing the subviews and I'm just doing something wrong? If not, is there any other way of accomplishing this?
推荐答案
这是正确的方法:
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
此方法是UIImage类的扩展方法,它也会照顾使图像在任何未来的高分辨率设备上看起来都很好。
This method is an extension method for UIImage class, and it will also take care of making the image looks good on any future high-resolution devices.
这篇关于需要将UIView捕获到UIImage中,包括所有子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!