DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Robert has posted 10 posts at DZone. View Full User Profile

Capture View As Image Function

09.01.2008
| 516 views |
  • submit to reddit
        // captures the passed view as an image

/+ (UIImage *)captureView:(UIView *)view 
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    
    UIGraphicsBeginImageContext( screenRect.size );
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect( ctx, screenRect );
    
    [view.layer renderInContext:ctx];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
 
    return newImage;
}