Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the contents of a UIView and change it into a UIImage?

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

like image 218
bobbypage Avatar asked Jun 19 '10 06:06

bobbypage


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do you add a UIImage in Objective C?

UIImage *img = [UIImage imageNamed:@"anyImageName"];

How do I find my UIImage path?

Once a UIImage is created, the image data is loaded into memory and no longer connected to the file on disk. As such, the file can be deleted or modified without consequence to the UIImage and there is no way of getting the source path from a UIImage.

What is UIImage?

An object that manages image data in your app.


1 Answers

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>
like image 87
v01d Avatar answered Oct 23 '22 11:10

v01d