我正在尝试使用以下代码捕获 cocoa WebView 的屏幕截图:
NSBitmapImageRep *bitmapRepRight = [[webView mainFrame].frameView bitmapImageRepForCachingDisplayInRect:cacheRect];
[[[webView mainFrame].frameView documentView] layout];
[[webView mainFrame].frameView cacheDisplayInRect:cacheRect toBitmapImageRep:bitmapRepRight];
return [bitmapRepRight CGImage];
此代码是从 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame 函数调用的。
我只获取页面的背景,但没有文本和图像。
如果我使用 [self PerformSelector:@selector(addPagesToCache) withObject:nil afterDelay:0.01]; 调用它然后它就可以工作,但这根本不是一个干净的方法。
可能是什么问题以及如何解决它?
最佳答案
如果您的 webview 是可见的(在窗口中),您可以使用 CGWindowListCreateImage 来获取窗口的部分图像。这也是一种解决方法,而不是完美的解决方案。
关于macos - 无法在 MacOS Web View 屏幕截图中获取所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13796062/