在 iOS 13 中获取 UIScrollView 的完整屏幕截图
Getting a full screenshot of a UIScrollView in iOS 13
我正在使用 Swift 5 和 iOS 13。我想截取整个滚动视图图像并打印出来,但底部显示为白色。我怎么解决这个问题?我在下面的图片中展示并解释了所有内容。
我也查了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()
}
}
这似乎是 iOS 13 个错误,但我在 this 答案中找到了解决方案。
解决方案是我从 superview 中删除 scrollview,然后 在截取屏幕截图后添加其旧约束。是的,这很愚蠢,但它就是这样工作的:(
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()
}
这是对我有用的代码片段。
extension UIView {
func snapshot(scrollView: UIScrollView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, false, UIScreen.main.scale)
layer.render(in: UIGraphicsGetCurrentContext()!)
let savedContentOffset = scrollView.contentOffset
let savedFrame = frame
scrollView.contentOffset = CGPoint.zero
frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
scrollView.contentOffset = savedContentOffset
frame = savedFrame
UIGraphicsEndImageContext()
return image
}
}
当你调用它时:
view.snapshot(scrollView: scrollView)
我正在使用 Swift 5 和 iOS 13。我想截取整个滚动视图图像并打印出来,但底部显示为白色。我怎么解决这个问题?我在下面的图片中展示并解释了所有内容。
我也查了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()
}
}
这似乎是 iOS 13 个错误,但我在 this 答案中找到了解决方案。
解决方案是我从 superview 中删除 scrollview,然后 在截取屏幕截图后添加其旧约束。是的,这很愚蠢,但它就是这样工作的:(
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()
}
这是对我有用的代码片段。
extension UIView {
func snapshot(scrollView: UIScrollView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, false, UIScreen.main.scale)
layer.render(in: UIGraphicsGetCurrentContext()!)
let savedContentOffset = scrollView.contentOffset
let savedFrame = frame
scrollView.contentOffset = CGPoint.zero
frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)
layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
scrollView.contentOffset = savedContentOffset
frame = savedFrame
UIGraphicsEndImageContext()
return image
}
}
当你调用它时:
view.snapshot(scrollView: scrollView)