我正在使用 Swift 5 和 iOS 13 。我正在尝试拍摄整个scrollview图像的屏幕截图并打印,但是底部显示为白色。我怎么解决这个问题?我展示并解释了以下图片中的所有内容。
我也被选中this topic
我的扩展代码是这样的:
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()
}
}
答案 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()
}