views:

33

answers:

3
+1  Q: 

Image from UIView

Hi,

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

Thanks, Andreas

+2  A: 

#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
Tim van Elsloo
Thanks! Thats what I was looking for.(You missed a ";")
Andreas Prang
Thanks, I updated my code :)!
Tim van Elsloo
A: 

if you have a hierarchy of sub UIViews you should use Apple's approach:

http://developer.apple.com/iphone/library/qa/qa2010/qa1703.html

bioffe
It's not working on iOS 3.2 devices (prior 4.0).
Andreas Prang
A: 

Ich habe den Ansatz genutzt und noch weiter verbessert. Leider brachte er mir nur ein gespiegeltes Bild.

Hier nun die vollständige Variante:

- (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;
}
Andreas Prang