我写了一个方法来获取WKWebView的屏幕截图 - 包括不可见的部分。这是我的代码:
var screenCapture: UIImage {
isUserInteractionEnabled = false
var screenShots = [UIImage]()
let numberOfShots = Int(ceil(contentSize.height / bounds.height))
print("SHOTS", numberOfShots)
for n in 0..<numberOfShots {
let dy = CGFloat(n) * bounds.height
scrollRectToVisible(CGRect(x: 0, y: dy, width: bounds.width, height: bounds.height), animated: false)
screenShots.append(screenShot())
}
let finalShot = stitchImages(images: screenShots, isVertical: true)
isUserInteractionEnabled = true
return finalShot
}
fileprivate func screenShot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0)
drawHierarchy(in: frame, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
stitchImages
是一种我也有的方法,但它有点长,而且我很确定它不是问题。
问题是只捕获了WKWebView可见区域周围的部分,但其余部分是白色的。
为什么这不起作用,我该如何解决?