如何完整的获取UIScrollView的内容截图?

废话不多说了,直接上Swift 代码吧。

extension UIScrollView {
    /// 内容截图
    var screenshotImage: UIImage? {
        var image: UIImage? = nil
        UIGraphicsBeginImageContext(contentSize)
        
        let formerContentOffset = contentOffset
        let formerFrame = frame
        self.contentOffset = .zero
        self.frame = CGRect(origin: CGPoint.zero, size: contentSize)
        
        UIGraphicsBeginImageContextWithOptions(contentSize, false, UIScreen.main.scale)
        if let currentContext = UIGraphicsGetCurrentContext() {
            self.layer.render(in: currentContext)
            image = UIGraphicsGetImageFromCurrentImageContext()
        }
        UIGraphicsEndImageContext()
        
        self.contentOffset = formerContentOffset
        self.frame = formerFrame
        
        return image
    }
}

推荐阅读更多精彩内容