I have simple UITableView with some data. The table\'s height is greater than the screen size. Now I need to catch screenshot of this table (a whole table). I k
I have same requirement to send image of tableview to admin via email, Hope this will help you. Function implemented in swift 4
func screenshot() -> UIImage{
var image = UIImage()
UIGraphicsBeginImageContextWithOptions(self.tblDetails.contentSize, false, UIScreen.main.scale)
// save initial values
let savedContentOffset = self.tblDetails.contentOffset
let savedFrame = self.tblDetails.frame
let savedBackgroundColor = self.tblDetails.backgroundColor
// reset offset to top left point
self.tblDetails.contentOffset = CGPoint(x: 0, y: 0)
// set frame to content size
self.tblDetails.frame = CGRect(x: 0, y: 0, width: self.tblDetails.contentSize.width, height: self.tblDetails.contentSize.height)
// remove background
self.tblDetails.backgroundColor = UIColor.clear
// make temp view with scroll view content size
// a workaround for issue when image on ipad was drawn incorrectly
let tempView = UIView(frame: CGRect(x: 0, y: 0, width: self.tblDetails.contentSize.width, height: self.tblDetails.contentSize.height))
// save superview
let tempSuperView = self.tblDetails.superview
// remove scrollView from old superview
self.tblDetails.removeFromSuperview()
// and add to tempView
tempView.addSubview(self.tblDetails)
// render view
// drawViewHierarchyInRect not working correctly
tempView.layer.render(in: UIGraphicsGetCurrentContext()!)
// and get image
image = UIGraphicsGetImageFromCurrentImageContext()!
// and return everything back
tempView.subviews[0].removeFromSuperview()
tempSuperView?.addSubview(self.tblDetails)
// restore saved settings
self.tblDetails.contentOffset = savedContentOffset
self.tblDetails.frame = savedFrame
self.tblDetails.backgroundColor = savedBackgroundColor
UIGraphicsEndImageContext()
return image
}