WKWebView CALayer图像导出空白图像 [英] WKWebView CALayer to image exports blank image

查看:225
本文介绍了WKWebView CALayer图像导出空白图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试截取网页的屏幕快照,但是图像始终为空白(白色).

I'm trying to take a screenshot of webpage but the image is always blank(white).

我正在使用此代码将CALayer转换为Data (从此处获取)

I'm using this code to convert CALayer to Data(taken from here)

extension CALayer {

/// Get `Data` representation of the layer.
///
/// - Parameters:
///   - fileType: The format of file. Defaults to PNG.
///   - properties: A dictionary that contains key-value pairs specifying image properties.
///
/// - Returns: `Data` for image.

func data(using fileType: NSBitmapImageFileType = .PNG, properties: [String : Any] = [:]) -> Data {
    let width = Int(bounds.width * self.contentsScale)
    let height = Int(bounds.height * self.contentsScale)
    let imageRepresentation = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSDeviceRGBColorSpace, bytesPerRow: 0, bitsPerPixel: 0)!
    imageRepresentation.size = bounds.size

    let context = NSGraphicsContext(bitmapImageRep: imageRepresentation)!

    render(in: context.cgContext)

    return imageRepresentation.representation(using: fileType, properties: properties)!
}

}

然后将数据作为.png

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) 
{
    let d = web.layer?.data() as NSData?  //web is the instance of WKWebView
    d!.write(toFile: "/Users/mac/Desktop/web.png", atomically: true)
}

但是我得到的是空白(白色)png,而不是我期望的

But I'm getting a blank(white) png instead of what I expected

1).我在做什么错了?

1). What am I doing wrong?

2).是否有其他可能的方法来获取图像的表示形式 网页(使用swift)?

2). Is there any other possible ways to get the image representation of webpage(Using swift)?

谢谢!

推荐答案

最新更新:

现在,您可以像WebView一样拍摄WKWebView的屏幕截图.

Now you can take screenshot of WKWebView just like WebView.

苹果为两个iOS都添加了新方法和macOS,

func takeSnapshot(with snapshotConfiguration: WKSnapshotConfiguration?, 
completionHandler: @escaping (NSImage?, Error?) -> Void)

但它仍处于测试阶段.

您无法拍摄WKWebView的屏幕截图.它总是返回空白图像.即使您尝试将WKWebView包含在另一个NSView中并进行截图,它也会为您提供空白图像代替WKWebView.

You can't take a screenshot of WKWebView. It always returns a blank image. Even if you try to include WKWebView inside another NSView and take a screenshot, it will give you blank image in place of WKWebView.

出于您的目的,应使用WebView而不是WKWebView.选中此问题.

You should use WebView instead of WKWebView for your purpose. Check this question.

如果可以使用私有框架(Apple不允许您的应用在其商店中使用),请检查此

If you are ok with using private frameworks(apple doesn't allow your app in its store), check this GitHub. Its written in Obj-C. I don't know Obj-C so I can't explain what's happening in that code. But it claims to do the work.

您最好的方法是使用WebView并在WebView上使用您提到的扩展名data().

Your best approach is to use WebView and use your mentioned extension data() on the WebView.

一个问题:为什么不使用phantomJS?

Just a question: Why don't you use phantomJS?

PS.这么晚才回复很抱歉.我没有看到您的电子邮件.

PS. Sorry for the late reply. I didn't see your e-mail.

这篇关于WKWebView CALayer图像导出空白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆