首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用硬件加速内容截图WKWebview

用硬件加速内容截图WKWebview
EN

Stack Overflow用户
提问于 2017-08-15 10:19:31
回答 1查看 1.2K关注 0票数 5

当有硬件加速的内容(一些特定的赌场游戏运行在iframe内)时,我在拍摄WKWebview内容的屏幕截图时遇到了严重的问题。到目前为止,我按照每个人的建议,使用了截图的标准方式:

代码语言:javascript
复制
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上的类似问题,开始记录屏幕上发生的一切,并在获得第一张图像后停止屏幕记录。

我已经经历了无数的堆栈溢出问题和解决方案,但它们都是在Obj(我完全不擅长的),过时或只是不够具体的我的需要。

现在我发现,我可以使用glReadPixels直接读取OpenGL中的像素(如果我的内容被硬件加速了,那么我就可以从显卡读取这些像素了,对吧?)

到目前为止,我已经成功地创建了一个Swift片段,可以执行类似于renderBuffer -> frameBuffer -> glReadPixels ->映像的操作。

代码语言:javascript
复制
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!我已经看到了许多关于如何让UIImage退出CAEAGLView的问题,但在我的例子中,不是CAEGLView,而是WKWebview。非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2018-11-13 01:51:21

我有一个简单的方法来拍摄快照,这是代码。

代码语言:javascript
复制
- (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);
        });
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45690932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档