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

2019-06-04  本文已影响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
    }
}

上一篇 下一篇

猜你喜欢

热点阅读