preface

Record the idea of solving this problem, originally thought this is a relatively simple implementation process, but I am still too young, crying to death!! To sum up: when encountering difficulties, we should search for ourselves and think independently.

The problem

Problem Description:

An unloaded cell was not captured when intercepting the contents of a UITableView and generating a long image.

Cause of the problem:

Due to the TableViewCell reuse problem, cells that are not captured during screenshot are not loaded.

To solve

Solution:

Since the cell is not loaded, we should first load the tableView and then take a screenshot of it. So I create a scrollView first and determine the content height to display. This height is the content height of the tableView. And then we’re going to intercept the scrollView.

Code:

Get tableView content screenshot, call directly.

func getTableViewScreenshot(tableView: UITableView.whereView: UIView) -> UIImage? {// Create a scrollView
    let scrollView = UIScrollView(a)// Set the color
    scrollView.backgroundColor = UIColor.white
    // Set the location
    scrollView.frame = whereView.bounds
    // Set the scroll position
    scrollView.contentSize = CGSize(width: screenWidth, height: tableView.contentSize.height)
    // Load the tableView into the view
    scrollView.addSubview(tableView)
    // Set the location
    tableView.snp.makeConstraints { (make) in
        make.top.left.right.equalTo(scrollView)
        make.width.equalTo(screenWidth)
        make.height.equalTo(tableView.contentSize.height)
    }
    /// Add to the specified view
    whereView.addSubview(scrollView)
    / / / screenshots
    let image = snapshotScreen(scrollView: scrollView)
    / / / remove the scrollView
    scrollView.removeFromSuperview()
    return image
}
Copy the code

The following is the screenshot method

func snapshotScreen(scrollView: UIScrollView) -> UIImage? {if UIScreen.main.responds(to: #selector(getter: UIScreen.scale)) {
        UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, false.UIScreen.main.scale)
    } else {
        UIGraphicsBeginImageContext(scrollView.contentSize)
    }
    
    let savedContentOffset = scrollView.contentOffset
    let savedFrame = scrollView.frame
    let contentSize = scrollView.contentSize
    let oldBounds = scrollView.layer.bounds
    
    if #available(iOS 13.*) {
        //iOS 13 requires a change in the Bounds of the TableView
        scrollView.layer.bounds = CGRect(x: oldBounds.origin.x, y: oldBounds.origin.y, width: contentSize.width, height: contentSize.height)
    }
    // The offset returns to zero
    scrollView.contentOffset = CGPoint.zero
    / / frame into contentSize
    scrollView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
    
    / / screenshots
    if let context = UIGraphicsGetCurrentContext() {
        scrollView.layer.render(in: context)
    }
    if #available(iOS 13.*) {
        scrollView.layer.bounds = oldBounds
    }
    let image = UIGraphicsGetImageFromCurrentImageContext(a)UIGraphicsEndImageContext(a)// Restore the frame and offset
    scrollView.contentOffset = savedContentOffset
    scrollView.frame = savedFrame
    return image
}
Copy the code