用它的孩子渲染 UIView [英] Rendering UIView with its children
问题描述
我有一个 UIView
,它有一个图像和一些按钮作为它的子视图.我想使用 renderInContext
或其他方法获取它的快照"图像.
I have a UIView
that has an image and some buttons as its subviews. I'd like to get a "snapshot" image of it using renderInContext
, or other method.
[clefView.layer renderInContext:mainViewContentContext];
如果我将它传递给我的 UIView
(如上所述),那么我会得到一个空白位图.没有孩子被渲染到位图中.
If I pass it my UIView
(as above) then I get a blank bitmap. None of the children are rendered into the bitmap.
如果我将作为图像的子视图传递给它,那么我将获得该位图的图像,并且不出所料,没有它的兄弟姐妹(按钮).
If I pass it the child view that is the image, then I get an image of that bitmap, and, unsurprisingly, none of its siblings (buttons).
我有点希望 renderInContext
能够获取图像和所有可见的子元素并将其渲染为位图.有没有人有任何想法如何做到这一点?
I was sort of hoping that renderInContext
would take the image and all it's visible children and render it into a bitmap. Has anyone have any ideas how to do this?
推荐答案
试试这个:
UIGraphicsBeginImageContext(clefView.bounds.size);
[clefView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这篇关于用它的孩子渲染 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!