Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image from UIView

is there any Possibility to create a image out of a UIView?

Thanks, Andreas

like image 699
Andreas Prang Avatar asked Aug 16 '10 16:08

Andreas Prang


2 Answers

#import "QuartzCore/QuartzCore.h" after you added the framework to your project. Then do:

UIGraphicsBeginImageContext(yourView.frame.size);
[[yourView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// The result is *screenshot
like image 120
cutsoy Avatar answered Jan 07 '23 05:01

cutsoy


I have used the answer to improve it even further. Unfortunately, the original code only produced a mirrored image.

So here's the working code:

- (UIImage *) imageFromView:(UIView *)view {

    UIGraphicsBeginImageContext(view.frame.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(currentContext, 0, view.size.height);
    // passing negative values to flip the image
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    [[appDelegate.scatterPlotView layer] renderInContext:currentContext];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenshot;
}
like image 35
Andreas Prang Avatar answered Jan 07 '23 06:01

Andreas Prang