Wednesday, March 17, 2010

Getting a screenshot of any View Programmatically

Place the code shown here in the main file of your view controller
-(IBAction)captureScreen:(id)sender
{
UIGraphicsBeginImageContext(foobar.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}

Here foobar is considered the UIView subclass which has the screen to be captured in the screenshot.