views:

216

answers:

2

Hello,

I currently have a view and I would like to change it into a UIImage. I would like to do this because the UIImage class is much better for what I need to do. How would you capture the contents of a UIView and copy the contents into a UIImage?

Thanks,

-David

+1  A: 

Here, try this

CGImageRef screen = UIGetScreenImage();
UIImage *screenImage = [UIImage imageWithCGImage:screen];

That will take a screenshot of the screen, so in theory capturing all of the view's elements and gives you a UIImage to work off of. Hope that helps!

Raphael Caixeta
Hey, that is quite cool but the problem is it captures the entire main view and I have a tab bar for example in my main view which I don't want in the final UIImage... Any way to get around this?Thanks in advance,-David
bobbypage
you could try cropping the UIImage to the exact x,y coordinates you'll need. If this isn't a good option for you, then this method probably won't work
Raphael Caixeta
+1  A: 

Like this:

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

You will need to include the CoreGraphics framework, and import the CALayer.h:

#import <QuartzCore/CALayer.h>
v01d