在iOS 13中获取UIScrollView的完整屏幕截图

时间:2020-03-20 13:48:47

标签: ios swift iphone scrollview core-graphics

我正在使用 Swift 5 iOS 13 。我正在尝试拍摄整个scrollview图像的屏幕截图并打印,但是底部显示为白色。我怎么解决这个问题?我展示并解释了以下图片中的所有内容。

我也被选中this topic

enter image description here

我的扩展代码是这样的:

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()

    }

}

1 个答案:

答案 0 :(得分:0)

这似乎是iOS 13错误,但我在this答案中找到了解决方案。

解决方案是我从超级视图中删除滚动视图,然后在截取屏幕截图后添加其旧约束。是的,这很愚蠢,但是它只是这样工作的:(

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()
}