美文网首页
如何完整的获取UIScrollView的内容截图?

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

作者: 卖码维生 | 来源:发表于2019-06-04 14:17 被阅读0次

废话不多说了,直接上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
    }
}

相关文章

网友评论

      本文标题:如何完整的获取UIScrollView的内容截图?

      本文链接:https://www.haomeiwen.com/subject/gbilxctx.html