OS X中的WKWebView cacheDisplayInRect屏幕截图

时间:2015-08-12 15:47:04

标签: objective-c macos cocoa wkwebview

我尝试制作WKWebView的截图是OS X应用程序,并获得完全空的截图。此代码适用于原始WebView,但不适用于WKWebView。

NSView *view = webView;
NSRect bounds = [view bounds];

NSBitmapImageRep *bitmapdata = [view bitmapImageRepForCachingDisplayInRect: bounds];
[webView cacheDisplayInRect: bounds toBitmapImageRep: bitmapdata];

任何想法如何解决?

1 个答案:

答案 0 :(得分:1)

现在,您可以使用WKWebView来截取屏幕截图。

Apple added a new method

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

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

但它还处于测试阶段。