Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the whole scrollable UITableView to PNG

I would like to save the whole UITableView to a PNG file.

With this code, I managed to save the visible part:

UIGraphicsBeginImageContextWithOptions(tableView.layer.frame.size, false, 0.0);
tableView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let data = UIImagePNGRepresentation(image)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let imageURL = documentsURL.URLByAppendingPathComponent("cached.png")
data!.writeToURL(imageURL, atomically: false)

But I would also like the scrollable parts which are not currently shown.

Any idea?

like image 657
Daniele B Avatar asked Jan 07 '23 04:01

Daniele B


1 Answers

Here are some Swift extensions files you can add to your project to make screenshots:

The new code would be:

let image = tableView.screenshot
let data = UIImagePNGRepresentation(image)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let imageURL = documentsURL.URLByAppendingPathComponent("cached.png")
data!.writeToURL(imageURL, atomically: false)
like image 51
Gagan_iOS Avatar answered Jan 15 '23 21:01

Gagan_iOS