gpt4 book ai didi

ios - iOS 13 不再支持捕获 UITableView/ScrollView(包括屏幕外部分)的屏幕截图

转载 作者:行者123 更新时间:2023-11-29 05:12:27 24 4
gpt4 key购买 nike

感谢gleb vodovozov's idea ,我可以捕获整个 TableView 的图像,包括屏幕外部分,但 super View 在使用它在 tempView 中渲染图像后不会将我的 TableView 恢复为“更改之前”的位置。

我仔细检查了他的代码,没有发现任何问题。也许只是因为 iOS 13 的“变化”,所以与以前的 iOS 版本兼容的代码现在出现了故障?

下面是他的代码

非常感谢任何帮助!

var image = UIImage()
UIGraphicsBeginImageContextWithOptions(self.myTable.contentSize, false, UIScreen.main.scale)

// save initial values
let savedContentOffset = self.myTable.contentOffset
let savedFrame = self.myTable.frame
let savedBackgroundColor = self.myTable.backgroundColor

// reset offset to top left point
self.myTable.contentOffset = CGPoint(x: 0, y: 0)
// set frame to content size
self.myTable.frame = CGRect(x: 0, y: 0, width: self.myTable.contentSize.width, height: self.myTable.contentSize.height)
// remove background
self.myTable.backgroundColor = UIColor.clear

// make temp view with scroll view content size
// a workaround for issue when image on ipad was drawn incorrectly
let tempView = UIView(frame: CGRect(x: 0, y: 0, width: self.myTable.contentSize.width, height: self.myTable.contentSize.height))

// save superview
let tempSuperView = self.myTable.superview
// remove scrollView from old superview
self.myTable.removeFromSuperview()
// and add to tempView
tempView.addSubview(self.myTable)

// render view
// drawViewHierarchyInRect not working correctly
tempView.layer.render(in: UIGraphicsGetCurrentContext()!)
// and get image
image = UIGraphicsGetImageFromCurrentImageContext()!

// and return everything back
tempView.subviews[0].removeFromSuperview()
tempSuperView?.addSubview(self.myTable)

// restore saved settings
self.myTable.contentOffset = savedContentOffset
self.myTable.frame = savedFrame
self.myTable.backgroundColor = savedBackgroundColor

UIGraphicsEndImageContext()

return image

最佳答案

// remove scrollView from old superview
self.myTable.removeFromSuperview()

来自documentation :

func removeFromSuperview(): Calling this method removes any constraints that refer to the view you are removing, or that refer to any view in the subtree of the view you are removing.

我知道您恢复了框架,但您可能需要在将其从 super View 中删除之前将约束保存在数组中,然后在再次将其添加到 super View 后再次添加它们。

// Save constraints
guard let superView = myTable.superView else {
return
}
var oldConstraints: [NSLayoutConstraint] = []
for constraint in superView.constraints {
if constraint.firstItem == myTable || constraint.secondItem == myTable {
oldConstraints.append(constraint)
}
}

self.myTable.removeFromSuperview()

// ...

tempSuperView?.addSubview(self.myTable)

// Restore old constraints
NSLayoutConstraint.activate(oldConstraints)

关于ios - iOS 13 不再支持捕获 UITableView/ScrollView(包括屏幕外部分)的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59554377/

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