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

I currently have a view, and I would like to change it to 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?

Thank,

-David

+5
source share
3 answers

Like this:

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

You will need to enable the CoreGraphics structure and import CALayer.h:

#import <QuartzCore/CALayer.h>
+7
source

Try here

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

This will take a screenshot, therefore, theoretically, capturing all the elements of the view and get UIImage to work. Hope this helps!

+2
source

UIView

- (UIImage*) capture {
    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
0

All Articles