私はウェブページのスクリーンショットを撮ろうとしていますが、画像は常に白です。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を使用しています)の画像表現を取得する他の方法はありますか?
ありがとうございました!
ありがとうございます!私はその質問について見ましたが、私はそのギター・レポを見たことはありませんでしたが、悲しいことに私もobjcを知らない。 – ssh
私はちょうどphantomJSの仕組みについて興味があります。 – ssh