Screenshot from a UITableView
This will get you the current visible region of the TableView.
If you want to render the entire tableview, first you need to be aware thats probably not a great idea for anything more than a few dozen rows because of memory and what not. The max image size in 2048x2048 so your tableView can be no taller than that.
UIView *viewToRender = self.tableView;
CGPoint contentOffset = self.tableView.contentOffset;
UIGraphicsBeginImageContext(viewToRender.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// KEY: need to translate the context down to the current visible portion of the tablview
CGContextTranslateCTM(ctx, 0, -contentOffset.y);
[viewToRender.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
take a look at this library I made to do exactly this, it has very useful methods like:
- Take a full screenshot of your UITableView (all the cells are rendered on a single UIImage)
- Take a screenshot for just a single cell given its indexPath
- Take a screenshot of just the headers, footers or rows of your UITableView
And more...
You can add it to your project via Cocoapods or manually by dragging the Source files.
The usage is so simple:
UIImage * tableViewScreenshot = [self.tableView screenshot];
I hope you find this library helpful as much as I do.