Skip to content

Instantly share code, notes, and snippets.

@nunogoncalves
Last active September 13, 2022 15:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nunogoncalves/e9ad98c239fa49f14af7d4c78f834041 to your computer and use it in GitHub Desktop.
Save nunogoncalves/e9ad98c239fa49f14af7d4c78f834041 to your computer and use it in GitHub Desktop.
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))
}
}
@maccoy54
Copy link

Hello,

I'm trying to use the code and I have a problem with this part with swift 5:
let shortMonthformatter = DateFormatter(format: "MM")
let longMonthFormatter = DateFormatter(format: "MMMM")
fileprivate let yearMonthFormatter = DateFormatter(format: "yyyy-MM")

An idea
Cordially
BACQ Js

@nunogoncalves
Copy link
Author

nunogoncalves commented Sep 13, 2022

Ah, that is probably an extension. Try this out @maccoy54 :

extension DateFormatter {
    init(format: String) {
       self.dateFormat = format
    }
}

But… what does this have to do with the gifts? 🤔 I believe you entered the wrong gist. Can you double check?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment