2017-06-16 18 views
1

私はウェブページのスクリーンショットを撮ろうとしていますが、画像は常に白です。WKWebView CALayerから画像をエクスポートする空白の画像

私はCALayer

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)! 
} 

} 

(taken from here) Dataに変換するためにこのコードを使用しています。そして .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。

1)。私は間違って何をしていますか?

2)。 ウェブページ(swiftを使用しています)の画像表現を取得する他の方法はありますか?

ありがとうございました!

答えて

1

最新のアップデート:

今、あなたはちょうどWebViewようWKWebViewのスクリーンショットを撮ることができます。 iOSとMacOSの、

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

しかし、そのまだベータ版の両方のための

Apple added new method


WKWebViewのスクリーンショットを撮ることはできません。常に空白の画像を返します。 WKWebViewを別のNSViewに含めると、スクリーンショットを撮ると、WKWebViewの代わりに空の画像が表示されます。

WKWebViewの代わりにWebViewを使用してください。これをチェックしてくださいquestion

プライベートフレームワークを使用しても問題ありません(Appleが店舗であなたのアプリを許可していない)場合は、GitHubにチェックを入れてください。 Obj-Cで書かれています。私はObj-Cを知らないので、そのコードで何が起きているのか説明できません。しかし、それは仕事をすると主張する。

WebViewを使用して、WebViewに記載されている内線番号data()を使用してください。

ちょっと質問:なぜあなたはphantomJSを使用しませんか?

PS。返事が遅れて申し訳ありません。私はあなたの電子メールを見ませんでした。

+0

ありがとうございます!私はその質問について見ましたが、私はそのギター・レポを見たことはありませんでしたが、悲しいことに私もobjcを知らない。 – ssh

+0

私はちょうどphantomJSの仕組みについて興味があります。 – ssh

関連する問題