Remees M Syde
Remees M Syde

Reputation: 2609

Unable to capture screen of WkWebView in Xamarin IOS

I am trying to capture the screen in IOS, Other than WkWebview all other view component I am able to capture by below code.WkWebview is giving a blank page as captured data. If I am using UIWebview the same code working Is there anything specific to do to take screen shot WkWebView.

Code for screen capture.

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 Configuration:

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();

Upvotes: 0

Views: 214

Answers (1)

Remees M Syde
Remees M Syde

Reputation: 2609

I fixed the issue by replacing the WKWebView with PdfView.I am using this view for loading PDFs.

The latest code is below

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;

Upvotes: 0

Related Questions