将具有多个子视图的视图截图为 wkwebview

Posted

技术标签:

【中文标题】将具有多个子视图的视图截图为 wkwebview【英文标题】:Taking screenshot of a view with multiple subviews as wkwebview 【发布时间】:2016-04-12 11:37:05 【问题描述】:

我在 ios 应用程序中有一个视图,其中添加了多个 wkwebview 作为子视图来呈现 html 内容。它显示了所有内容,但是当我尝试截屏时,它显示为空白,好像内容从未在 wkwebviews 中呈现。

我用来截图的代码:

 //view is parent view which contains wkwebviews as childviews
 func takeScreenShot(view: UIView) -> UIImage
    UIGraphicsBeginImageContext(view.bounds.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image


当我检查 UI Hierarchy 时,wkwebviews 的内容实际上超出了父视图的范围。有合适的截图方法吗?

【问题讨论】:

【参考方案1】:

你试过了吗:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage* uiImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

取自here

【讨论】:

我试过这些代码行。它冻结了 UI。

以上是关于将具有多个子视图的视图截图为 wkwebview的主要内容,如果未能解决你的问题,请参考以下文章