使用硬件加速内容的 WKWebview 截屏

6

当在WKWebview中遇到硬件加速内容时(一些嵌入在iframe中运行的赌场游戏),我在截取屏幕截图方面遇到了严重问题。 目前,我使用了所有人都建议的标准截图方法:

UIGraphicsBeginImageContextWithOptions(containerView.frame.size, true, 0.0)

containerView.layer.render(in: UIGraphicsGetCurrentContext()!)

//This line helps to fix view rendering for taking screenshot on older iOS devices
containerView.drawHierarchy(in: containerView.bounds, afterScreenUpdates: true)

let image = UIGraphicsGetImageFromCurrentImageContext()!

UIGraphicsEndImageContext()

这种方法非常好用,直到我在WKWebview中有一些由GPU渲染的内容。GPU渲染的内容在截屏时呈现为黑色。我尝试了所有可能的技巧,但没有任何帮助。即使是XCode视图层次结构调试器也无法显示那些硬件加速的内容。因此,与Android类似,我需要另一个截图方式我已经通过开始记录屏幕上发生的所有事情并在获取第一张图像后停止屏幕录制来解决了Android上的类似问题。

我查看了很多Stack Overflow的问题和解决方案,但它们大多数是Obj-C(我完全不擅长),过时或者对我的需求不够具体。

现在我发现,我可以使用glReadPixels直接从OpenGL读取像素(如果我的内容是硬件加速的,那么从显卡读取这些像素是有意义的,对吗?)

到目前为止,我已经成功创建了一个Swift代码片段,类似于renderBuffer -> frameBuffer -> glReadPixels -> image

let width = Int(containerView.frame.size.width)
let height = Int(containerView.frame.size.height)

//BeginImageContext code was run above

let api = EAGLRenderingAPI.openGLES3
let context2 = EAGLContext(api: api)
EAGLContext.setCurrent(context2)

// Setup render buffer
var renderBuffer : GLuint = GLuint()

let size = GLsizei(10)
glGenRenderbuffers(size, &renderBuffer)
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), renderBuffer)
let bufferWidth = GLsizei(width * 1)
let bufferHeight = GLsizei(height * 1)
let bufferFormat = GLenum(GL_RGBA8)
glRenderbufferStorage(GLenum(GL_RENDERBUFFER), bufferFormat, bufferWidth, bufferHeight)

// Setup frame buffer
var frameBuffer = GLuint()
glGenFramebuffers(GLsizei(10), &frameBuffer)
glBindFramebuffer(GLenum(GL_FRAMEBUFFER), frameBuffer)
glFramebufferRenderbuffer(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_RENDERBUFFER), renderBuffer)

// Draw
glReadBuffer(GLenum(GL_RENDERBUFFER))
glClearColor(0.1, 0.2, 0.3, 0.2)
glClear(GLbitfield(GL_COLOR_BUFFER_BIT))

//--------------
let bytes = malloc(width*height*4)
let bytes2 = malloc(width*height*4)

let x : GLint = GLint(0)
let y : GLint = GLint(0)
let w : GLsizei = GLsizei(width)
let h : GLsizei = GLsizei(height)

glReadPixels(x, y, w, h, GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), bytes)

let data = NSData(bytes: bytes, length: width * height * 4)
let dataProvider = CGDataProvider(data: data)
//let dataProvider2 = CGDataProvider(dataInfo: nil, data: bytes!, size: width * height * 4, releaseData: )
let colorspace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo: CGBitmapInfo = [.byteOrder32Little, CGBitmapInfo(rawValue: CGImageAlphaInfo.last.rawValue)]
let aCGImage = CGImage(
    width: Int(width),
    height: Int(height),
    bitsPerComponent: 8,
    bitsPerPixel: 32,
    bytesPerRow: 4 * Int(width),
    space: colorspace,
    bitmapInfo: bitmapInfo,
    provider: dataProvider!,
    decode: nil, 
    shouldInterpolate: true,
    intent: .defaultIntent
    )!
let imaag = UIImage(cgImage: aCGImage)
//I get the image of the same color that is defined at clearColor

我的问题是,我这样做是否正确?是否有可能将我的WKWebview(或将其拍摄为UIView)以某种方式渲染到renderBuffer / frameBuffer中,以便 glReadPixels可以实际读取屏幕上的内容?

PS!我已经看到很多关于从CAEAGLView获取UIImage的问题,但在我的情况下,它不是CAEGLView而是WKWebview。非常感谢。

1个回答

0
我有一个简单的方法来获取快照,这是代码。
- (void)screenLoogShoot:(void (^)(UIImage *fullImage))snapShotHandler {
    WKWebView *webView = self.webView;
    UIScrollView *scrollView = webView.scrollView;
    CGFloat boundsWidth = scrollView.bounds.size.width;
    CGFloat contentHeight = scrollView.contentSize.height;
    CGFloat scale = [UIScreen mainScreen].scale;
    CGRect oldFrame = self.webView.frame;

    if (@available(iOS 11.0, *)) {
        self.webView.frame = CGRectMake(0, 0, boundsWidth, contentHeight);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            WKSnapshotConfiguration *configuration = [WKSnapshotConfiguration new];
            configuration.rect = CGRectMake(0, 0, boundsWidth, contentHeight);
            configuration.snapshotWidth = @(boundsWidth);
            [self.webView takeSnapshotWithConfiguration:configuration completionHandler:^(UIImage * _Nullable snapshotImage, NSError * _Nullable error) {
                UIGraphicsBeginImageContextWithOptions(CGSizeMake(boundsWidth, contentHeight), NO, scale);
                [snapshotImage drawInRect:CGRectMake(0, 0, boundsWidth, contentHeight)]; 
                UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                self.webView.frame = oldFrame;
                snapShotHandler(fullImage);
            }];
        });
    } else {
        self.webView.frame = CGRectMake(0, 0, boundsWidth, contentHeight);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(webView.bounds.size.width, webView.bounds.size.height), NO, scale);
            [webView drawViewHierarchyInRect:CGRectMake(0, 0, boundsWidth, contentHeight) afterScreenUpdates:YES];
            UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            self.webView.frame = oldFrame;
            !snapShotHandler ? : snapShotHandler(fullImage);
        });
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接