Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scrollable screenshots
import UIKit
class ViewController: UIViewController {
let tableView: UITableView = {
let tv = UITableView(frame: .zero, style: .plain)
tv.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
return tv
}()
override func loadView() {
self.view = tableView
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.view.window?.windowScene?.screenshotService?.delegate = self
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 100 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("Cell #\(indexPath.row)")
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = "Cell #\(indexPath.row)"
return cell
}
}
extension ViewController: UIScreenshotServiceDelegate {
func screenshotService(_ screenshotService: UIScreenshotService, generatePDFRepresentationWithCompletion completionHandler: @escaping (Data?, Int, CGRect) -> Void) {
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, .zero, nil)
UIGraphicsBeginPDFPageWithInfo(.init(origin: .zero, size: tableView.contentSize), nil)
if let context = UIGraphicsGetCurrentContext() {
let frame = tableView.frame
let contentOffset = tableView.contentOffset
let contentInset = tableView.contentInset
tableView.contentOffset = .zero
tableView.contentInset = .zero
tableView.frame = .init(origin: .zero, size: tableView.contentSize)
tableView.layer.render(in: context)
tableView.frame = frame
tableView.contentOffset = contentOffset
tableView.contentInset = contentInset
}
UIGraphicsEndPDFContext()
let y = tableView.contentSize.height - tableView.contentOffset.y - tableView.frame.height
completionHandler(data as Data, 0, .init(origin: CGPoint(x: 0, y: y), size: view.frame.size))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.