I've facing problems in removing the grey areas from the screenshot of the tableview.
This is the output when I tried to screenshot a UITableView
(I actually hide some of the tableViewCell
that's not needed, probably that's why area appears grey colour)
Any help is appreciated!
Thank you very much! :)
Answer
I found the solution by using @kishan and @Sneak's answer.
As I've screenshot my UITableView
programmatically, I just changed the view's and tableview's backgroundColor
to white in the codes.
func screenshot() -> UIImage{
var image = UIImage();
UIGraphicsBeginImageContextWithOptions(self.tableView.contentSize, false, UIScreen.main.scale)
// save initial values
let savedContentOffset = self.tableView.contentOffset;
let savedFrame = self.tableView.frame;
let savedBackgroundColor = self.tableView.backgroundColor
// reset offset to top left point
self.tableView.contentOffset = CGPoint(x: 0, y: 0);
// set frame to content size
self.tableView.frame = CGRect(x: 0, y: 0, width: self.tableView.contentSize.width, height: self.tableView.contentSize.height);
// set background to white
//-->// self.tableView.backgroundColor = UIColor.white
// 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.tableView.contentSize.width, height: self.tableView.contentSize.height));
//-->//tempView.backgroundColor = UIColor.white
// save superview
let tempSuperView = self.tableView.superview
// remove scrollView from old superview
self.tableView.removeFromSuperview()
// and add to tempView
tempView.addSubview(self.tableView)
// 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.tableView)
// restore saved settings
self.tableView.contentOffset = savedContentOffset;
self.tableView.frame = savedFrame;
self.tableView.backgroundColor = savedBackgroundColor
UIGraphicsEndImageContext();
return image
}
try this hope its work :)
Here what you see gray color is the footerview of tableview, so you can make that view nil or initialize it like this
This may solve your issue...