N.J
N.J

Reputation: 1230

(Xamarin.Forms IOS) PDF image not displaying

I have a cross platform application created in Xamrin Forms. The Application works perfectly in Android and UWP. But when loading in IOS the icons are not visible.
For example

<Image >
      <Image.Source>
          <OnPlatform x:TypeArguments="FileImageSource">
              <On Platform="Android" Value="login_icon" />
              <On Platform="UWP" Value="Images/login_icon.png" />
              <On Platform="iOS" Value="login_icon" />
          </OnPlatform>
       </Image.Source>
</Image>   

I have added all my images in the PDF format in the Assest Catalog. The size of the images are also correct. But for some reason the icons are not displaying.
However if i add these images into Resources as .png they appear. The offical document says this approch is deprecated.

EDIT: Everything looks good in the simulator but when i test using a real device, these icons are not visible.

Upvotes: 1

Views: 365

Answers (1)

Adrain
Adrain

Reputation: 1934

You can use CutomRenderer to create a custom webview to display pdf file.

code like:

    public class CustomWebView : WebView
    {
        public static readonly BindableProperty UriProperty = BindableProperty.Create (propertyName:"Uri",
                returnType:typeof(string),
                declaringType:typeof(CustomWebView),
                defaultValue:default(string));
    
        public string Uri {
            get { return (string)GetValue (UriProperty); }
            set { SetValue (UriProperty, value); }
        }
    }

In IOS:

   [assembly: ExportRenderer (typeof(CustomWebView), typeof(CustomWebViewRenderer))]
    namespace DisplayPDF.iOS
    {
        public class CustomWebViewRenderer : ViewRenderer<CustomWebView, UIWebView>
        {
            protected override void OnElementChanged (ElementChangedEventArgs<CustomWebView> e)
            {
                base.OnElementChanged (e);
    
                if (Control == null) {
                    SetNativeControl (new UIWebView ());
                }
                if (e.OldElement != null) {
                    // Cleanup
                }
                if (e.NewElement != null) {
                    var customWebView = Element as CustomWebView;
                    string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", WebUtility.UrlEncode (customWebView.Uri)));
                    Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false)));
                    Control.ScalesPageToFit = true;
                }
            }
        }
    }

Here is the document about customrenderer https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/introduction

Upvotes: 0

Related Questions