How to take screenshot of UIScrollView visible area?

前端 未结 8 1610
鱼传尺愫
鱼传尺愫 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:34

    Jeffery Sun has the right answer. Just put your scroll view inside another view. Get container view to render in context. done.

    In the code below, cropView contains the scroll view to be captured. The solution is really just that simple.

    As I understand the question and why I found this page, the whole content of the scroll view isn't wanted - just the visible portion.

    func captureCrop() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.cropView.frame.size, true, 0.0)
        self.cropView.layer.renderInContext(UIGraphicsGetCurrentContext())
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image;
    }
    

提交回复
热议问题