gpt4 book ai didi

macos - 将离屏 WKWebView 渲染成 NSImage

转载 作者:行者123 更新时间:2023-12-03 16:09:38 46 4
gpt4 key购买 nike

我尝试使用将离屏 WKWebView 渲染为图像

  • func cacheDisplayInRect(rect: NSRect, toBitmapImageRep bitmapImageRep: NSBitmapImageRep)
  • func drawLayer(layer: CALayer, inContext ctx: CGContext)

没有成功。生成的图像始终为空(白色或透明)。有人设法在优胜美地做到这一点吗?

最佳答案

您可以使用drawViewHierarchyInRect来做到这一点:其他方法似乎都不起作用,像这样使用它:

UIGraphicsBeginImageContextWithOptions(newRect.size, YES, 0);
BOOL ok = [view drawViewHierarchyInRect:newRect afterScreenUpdates:YES];
if (!ok) {
NSLog(@"Problem with drawView...");
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我在 iOS 中做了同样的事情,但不幸的是这个方法很慢,也必须在主线程中运行,并且只有在 afterScreenUpdates 设置为 yes 时才有效。请参阅此答案:How can I take a snapshot of a UIView that isn't rendered?

此外,从我所看到的情况来看,无法判断网页的任何方面是否需要重绘。

关于macos - 将离屏 WKWebView 渲染成 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30149373/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com