I wrote a method to get a screen capture of a WKWebView––including the part that is not visible. Here is my code for that:
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
is a method that I also have, but it is sort of long and I'm pretty sure it is not the problem.
The problem is that only the part around the visible area of the WKWebView is captured, but the rest is white.
Why is this not working, and how can I fix it?