views:

1620

answers:

1

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?

-mz

+5  A: 

Try something like this:

UIGraphicsBeginImageContext(clefView.bounds.size);
[clefView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
rincewind
Thanks, but that didn't do it. I get the same results. The problem, I think, is that renderInContext isn't traversing the subviews and only doing the top layer that is passed to it (despite what I think it supposed to do). Do I have to call renderInContext for each subview?
mahboudz
Ok, playing with it a bit more and this is the right answer. My ImageContext was being set to a sibling view not a superview.Thanks!
mahboudz
This works much better than any other method I have encountered.
freespace
`#import <QuartzCore/QuartzCore.h>` to avoid warnings
slf