I want to take a screenshot with WKWebView
contains a playing video. I tried many ways, it works well in Simulator, but on device the video's content is not captured.
Simulator:
Device:
I tried the following ways:
CALayer#render
let window = UIApplication.shared.windows.first { $0.isKeyWindow } if window == nil { return } let layer = window!.layer let screenRect = window!.screen.bounds UIGraphicsBeginImageContext(screenRect.size) let ctx:CGContext = UIGraphicsGetCurrentContext()! layer.render(in: ctx) self.screenShotImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()
and
UIView#drawHierarchy
let window = UIApplication.shared.windows.first { $0.isKeyWindow } if window == nil { return } UIGraphicsBeginImageContextWithOptions(window!.frame.size, false, 0) window!.drawHierarchy(in: window!.frame, afterScreenUpdates: true) self.screenShotImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()
and
WKWebView#takeSnapshot
let config = WKSnapshotConfiguration() config.rect = WKWebViewHolder.webView!.frame config.afterScreenUpdates = false webView.takeSnapshot(with: config, completionHandler: { (image: UIImage?, error: Error?) in if error != nil { return } self.screenShotImage = image })
What should I do? 🤔