当需要截取 TableView 或 ScrollView 中的全部内容时,可以采用以下方法:
/// 长截屏
func fullScreenShot(_ scrollView: UIScrollView) -> UIImage {
// 记录当前 scrollView 的"偏移量"和"坐标"
let savedContentOffset = scrollView.contentOffset
let savedScrollViewFrame = scrollView.frame
scrollView.contentOffset = .zero
scrollView.frame = CGRect(origin: .zero, size: scrollView.contentSize)
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, true, UIScreen.main.scale)
scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()!
// 重置
scrollView.contentOffset = savedContentOffset
scrollView.frame = savedScrollViewFrame
UIGraphicsEndImageContext()
return image
}