【问题标题】:Unable to capture screen of WkWebView in Xamarin IOS无法在 Xamarin IOS 中捕获 WkWebView 的屏幕
【发布时间】:2019-09-26 05:22:53
【问题描述】:

我正在尝试在 IOS 中捕获屏幕,除了 WkWebview 之外,我可以通过下面的代码捕获所有其他视图组件。WkWebview 正在提供一个空白页面作为捕获的数据。如果我使用 UIWebview 相同的代码工作是否有任何特定的操作来截取 WkWebView 的屏幕截图。

截屏代码。

public static UIImage SnapshotView(this UIView view)
{
   UIGraphics.BeginImageContextWithOptions(view.Bounds.Size, false, UIScreen.MainScreen.Scale);
    view.DrawViewHierarchy(view.Bounds, true);

    var image = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();

     return image;
 }

WkWebView 配置:

WKWebView _wkWebView = new WKWebView(ReaderView.Frame, new WKWebViewConfiguration());

_wkWebView.LoadFileUrl(tempUrl, tempUrl);
_wkWebView.ContentMode = UIViewContentMode.ScaleToFill;
_wkWebView.BackgroundColor = UIColor.Clear;
_wkWebView.Opaque = false;
_wkWebView.ScrollView.BackgroundColor = UIColor.Clear;

//_wkWebView.DrawViewHierarchy(_wkWebView.Bounds, true);
ReaderView.AddSubview(_wkWebView);
var imag = _wkWebView.SnapshotView();

【问题讨论】:

  • 如果DrawViewHierarchy没有被注释掉,它应该可以工作(假设html页面已经完成)
  • 您是否尝试使用 PerformSelector 调用方法 SnapshotView() - 延迟
  • 我也尝试过使用“DrawViewHierarchy”,然后我才将其注释掉。
  • @Prasanth,您能否提供示例代码或实现链接。
  • 这样检查一次,NSTimer timer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(3), delegate {var imag = _wkWebView.SnapshotView();});

标签: c# ios xamarin webview wkwebview


【解决方案1】:

我通过用 PdfView 替换 WKWebView 解决了这个问题。我正在使用这个视图来加载 PDF。

最新代码如下

pdfView = new PdfView();
            pdfView.TranslatesAutoresizingMaskIntoConstraints = false;
            ReaderView.AddSubview(pdfView);

            pdfView.LeadingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.LeadingAnchor).Active = true;
            pdfView.TrailingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TrailingAnchor).Active = true;
            pdfView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor).Active = true;
            pdfView.BottomAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.BottomAnchor).Active = true;

            //  var path = Bundle.main.url(forResource: "test", withExtension: "pdf") else { return }

            PdfDocument document;
            // PdfDocument
            using (urlString = new NSString(FilePath))
            using (var tempUrl = NSUrl.CreateFileUrl(new string[] { urlString }))
                document = new PdfDocument(tempUrl);


            //if var document = PdfDocument(url: path) {
            pdfView.Document = document;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2012-04-19
    • 2021-08-30
    • 2018-01-18
    相关资源
    最近更新 更多