我想捕獲webview顯示給用戶的圖像,所以我可以對網頁進行一些顏色分析。當我試圖讓圖像從它的父,我基本上得到一個白色的盒子,即使頁面已經呈現:WKWebView Screenshots
func makeImageSnapshot()-> (NSImage)
{
let imgSize = self.view.bounds.size
let bir = self.viewbitmapImageRepForCachingDisplayInRect(self.webView!.view.bounds)
bir.size = imgSize
self.webView.cacheDisplayInRect(self.view.bounds, toBitmapImageRep:bir)
let image = NSImage(size:imgSize)
image.addRepresentation(bir)
self.image = image
return image
}
func saveSnapshot()
{
let imgRep = self.image!.representations[0]
let data = imgRep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: nil)
data.writeToFile("/tmp/file.png", atomically: false)
}
它看起來對我來說,我無法訪問實際的性能在webView中查看(在這種情況下是邊界)。當我嘗試訪問它時,編譯器barfs:
/Users/josh/Canary/MacOsCanary/canary/canary/Modules/Overview/Overview.swift:55:37:'(NSView !, stringForToolTip:NSToolTipTag,point :NSPoint,userData:UnsafePointer <()>) - > String!'沒有名爲'bounds'的成員
我的猜測是,這是由於OS X和iOS使用的擴展方法而發生的。任何想法,或者我應該回到使用傳統的WebView?
您試圖訪問字符串上的「邊界」! – nhgrif
@Josh你有沒有找到iOS的解決方案?目前我正在使用snapshotViewAfterScreenUpdates。但是我必須在渲染完webview後調用它,所以我在didFinishNavigation中等待0.1秒,然後調用snapshotViewAfterScreenUpdates。不太方便和可靠( – sidslog