How to take screenshot of UIScrollView visible area?

前端 未结 8 1861
鱼传尺愫
鱼传尺愫 2021-02-13 16:57

How do I take a 1:1 screenshot of UIScrollView visible area? The content may be larger or smaller than UIScrollView bounds as well as half-hidden (I\'ve implemented custom scrol

8条回答
  •  醉话见心
    2021-02-13 17:48

    Swift 4 version of Abduliam Rehmanius answer as UIScrollView extension with translation, no slow cropping

    extension UIScrollView {
    
        var snapshotVisibleArea: UIImage? {
            UIGraphicsBeginImageContext(bounds.size)
            UIGraphicsGetCurrentContext()?.translateBy(x: -contentOffset.x, y: -contentOffset.y)
            layer.render(in: UIGraphicsGetCurrentContext()!)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    }
    

提交回复
热议问题