Getting a screenshot of a UIScrollView, including offscreen parts

前端 未结 12 2119
遥遥无期
遥遥无期 2020-11-22 17:31

I have a UIScrollView decendent that implements a takeScreenshot method that looks like this:

-(void)takeScreenshot {  
  CGRect contextRect  =          


        
12条回答
  •  伪装坚强ぢ
    2020-11-22 17:55

    SWIFT 3 version:

    func snapshot() -> UIImage?
    {      
        UIGraphicsBeginImageContext(scrollView.contentSize)
    
        let savedContentOffset = scrollView.contentOffset
        let savedFrame = scrollView.frame
    
        scrollView.contentOffset = CGPoint.zero
        scrollView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
    
        scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
    
        scrollView.contentOffset = savedContentOffset
        scrollView.frame = savedFrame
    
        UIGraphicsEndImageContext()
    
        return image
    }
    

    This worked for me

提交回复
热议问题