如何使用嵌入视频从 WKWebView 截取屏幕截图?

Posted

技术标签:

【中文标题】如何使用嵌入视频从 WKWebView 截取屏幕截图?【英文标题】:How to take a screenshot from WKWebView with embedded video? 【发布时间】:2021-07-02 03:23:08 【问题描述】:

我想从WKWebView 截取一个包含正在播放的视频的屏幕截图。我尝试了很多方法,它们在模拟器上运行良好,但无法捕获设备上的视频内容。无法在设备上录制视频内容:

模拟器: Simulator

设备: Device


我尝试了以下方法:

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()

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()

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
)

有人可以帮助我吗? ????

【问题讨论】:

【参考方案1】:

你可以试试下面的代码截图:

详情 Xcode 9.3,Swift 4.1。 Xcode 10.2 (10E125) 和 11.0 (11A420a)、Swift 5。 在 ios 上测试:9、10、11、12、13

解决方案

import UIKit

extension UIApplication 

    func getKeyWindow() -> UIWindow? 
        if #available(iOS 13, *) 
            return windows.first  $0.isKeyWindow 
         else 
            return keyWindow
        
    

    func makeSnapshot() -> UIImage?  return getKeyWindow()?.layer.makeSnapshot() 



extension CALayer 
    func makeSnapshot() -> UIImage? 
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
        defer  UIGraphicsEndImageContext() 
        guard let context = UIGraphicsGetCurrentContext() else  return nil 
        render(in: context)
        let screenshot = UIGraphicsGetImageFromCurrentImageContext()
        return screenshot
    


extension UIView 
    func makeSnapshot() -> UIImage? 
        if #available(iOS 10.0, *) 
            let renderer = UIGraphicsImageRenderer(size: frame.size)
            return renderer.image  _ in drawHierarchy(in: bounds, afterScreenUpdates: true) 
         else 
            return layer.makeSnapshot()
        
    


extension UIImage 
    convenience init?(snapshotOf view: UIView) 
        guard let image = view.makeSnapshot(), let cgImage = image.cgImage else  return nil 
        self.init(cgImage: cgImage, scale: image.scale, orientation: image.imageOrientation)
    

用法

imageView.image = UIApplication.shared.makeSnapshot()

// or
imageView.image = view.makeSnapshot()

// or
imageView.image = view.layer.makeSnapshot()

// or
imageView.image = UIImage(snapshotOf: view)

【讨论】:

感谢您的帮助,但它不起作用。视频区域为黑色,像这样:i.stack.imgur.com/WZfvT.png

以上是关于如何使用嵌入视频从 WKWebView 截取屏幕截图?的主要内容,如果未能解决你的问题,请参考以下文章

mac如何截屏

在手机上如何从视频中截取想要的照片

手机截屏长屏如何截

C#制作截图软件时,怎样不截取软件窗口本身? 看下图:软件自身也被截下来了

怎么从视频中提取一张照片

mac如何截屏并保存