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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With