Search code examples
iosswiftiphonescrollviewcore-graphics

Getting a full screenshot of a UIScrollView in iOS 13


I am using Swift 5 and iOS 13. I am trying to take a screenshot of the entire scrollview image and print it, but the bottom appears in white. How can i solve this problem? I showed and explained everything in the picture below.

I am also checked this topic

enter image description here

My extension code like this:

extension UIScrollView {

    func screenshot() -> UIImage {

        let savedContentOffset = contentOffset
        let savedFrame = frame

        UIGraphicsBeginImageContextWithOptions(contentSize, false, 0)

        contentOffset = .zero
        frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)
        layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        contentOffset = savedContentOffset
        frame = savedFrame

        return image ?? UIImage()

    }

}

Solution

  • It's seems like iOS 13 bug but I found the solution in this answer.

    The solution is I am removing scrollview from superview and then adding in with its old constraints after taking the screenshot. Yeah, it's stupid but it just works that way :(

    scrollView.removeFromSuperview()
    let print = scrollView.screenshot()
    mainView.addSubview(scrollView)
    scrollView.snp.makeConstraints { (make) in
        make.top.equalTo(topView.snp.bottom)
        make.left.bottom.right.equalToSuperview()
    }