Swift 3: How to remove the grey areas of the scree

2019-07-30 01:25发布

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
}

2条回答
Root(大扎)
2楼-- · 2019-07-30 02:00

try this hope its work :)

tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
查看更多
Juvenile、少年°
3楼-- · 2019-07-30 02:08

Here what you see gray color is the footerview of tableview, so you can make that view nil or initialize it like this

tableView.tableFooterView = UIView() 

This may solve your issue...

查看更多
登录 后发表回答