首页 > 解决方案 > Xamarin - iOS - WKWebView 快照不适用于加载的 PDF

问题描述

我正在尝试将 WKWebView 内容转换为图像。它在我加载的任何 URL 上都可以正常工作,但是当我尝试获取加载的 PDF 的图像时,它会给出空白的灰色图像。以下是我迄今为止尝试过的一些方法以及视频链接

WKWebView 到带有 PDF 的图像:https ://drive.google.com/file/d/1xPNk10HEd-02YpXfH-NRpQzqT6mSQFRd/view?usp=sharing

WKWebView 与站点的图像:https ://drive.google.com/file/d/14YNqHYmDweMW8sE3SHvHIzLd2fOyko6l/view?usp=sharing

    // Method 1
    UIImage image;
    UIGraphics.BeginImageContextWithOptions(webView1.Frame.Size, true, 0.0f);        
    webView1.DrawViewHierarchy(webView1.Bounds, afterScreenUpdates: true);
    image = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();
    return image;



    // Method 2
    UIImage image;
    UIGraphics.BeginImageContextWithOptions(webView1.Frame.Size, true, 0.0f);        
    webView1.Layer.RenderInContext(UIGraphics.GetCurrentContext());
    image = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();
    return image;



    // Method 3
    UIImage image = null;
    var config = new WKSnapshotConfiguration()
    {
        Rect = webView1.Bounds,
    };

    webView1.TakeSnapshot(config, (img, error) =>
    {
        if (error == null)
        {
            image = img;
            AttachImageIntoMessage(image);
        }
        else
            System.Diagnostics.Debug.WriteLine(error.ToString());
    });

以上所有方法在简单链接上都可以正常工作,例如https://www.google.com,但是当 WKWebView 加载 PDF 时,它们都不起作用,例如https://blog.mozilla.org/security/files/2015/ 05/HTTPS-FAQ.pdf

标签: iospdfxamarinxamarin.ioswkwebview

解决方案


推荐阅读