gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:15:22 24 4
gpt4 key购买 nike

我正在使用 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()

}

}

最佳答案

这是对我有用的代码片段。

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)

关于ios - 在 iOS 13 中获取 UIScrollView 的完整屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60775473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com