Rendering UIView with its children

mahboudz picture mahboudz · Apr 25, 2009 · Viewed 9.5k times · Source

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];

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).

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?

Answer

rincewind picture rincewind · Apr 25, 2009

Try something like this:

UIGraphicsBeginImageContext(clefView.bounds.size);
[clefView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();