xamarinxamarin.androidxamarin.iosffimageloading

File placement and Build type for SVG's in Android and iOS


I had a question concerning the documentation for Xamarin.Native SVG support here:

In the sample code it calls LoadFile:

ImageService.Instance
    .LoadFile("image.svg")
    .
    .

In order for this to work, where in the project must image.svg be placed? And what is the Build Type for image.svg (Content, AndroidRersource, etc.)? And is it the same behavior for iOS?

I'm having trouble getting it to work. I placed my svg in the drawable directory with a build type of AndroidResource.

Android Project
    |
    |
    -> Resources
        |
        |
        -> drawable
            |
            |
            -> image.svg

Solution

  • ImageService.Instance.LoadFile: It used for loading files from the disk (full path only).

    You should use LoadFileFromApplicationBundle(application bundle), OR LoadCompiledResource(application resource).

    Load an image from a file from application bundle.

              /// <summary>
              /// Load an image from a file from application bundle.
              /// eg. assets on Android, compiled resource for other platforms
              /// </summary>
              /// <returns>The new TaskParameter.</returns>
              /// <param name="filepath">Path to the file.</param>
              TaskParameter LoadFileFromApplicationBundle(string filepath);
    

    Xamarin.Android

    xml:

      <FFImageLoading.Views.ImageViewAsync
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    Assets: with AndroidAsset of Build Action

    enter image description here

    MainActivity:

     var imageView = FindViewById<ImageView>(Resource.Id.imageView);
            var filePath = "sample2.svg";
            ImageService.Instance.LoadFileFromApplicationBundle(filePath).WithCustomDataResolver(new SvgDataResolver(64, 0, true)).Into(imageView);
    

    Load an image from a file from application resource. Use the name of the resource in drawable folder without extension.

              /// <summary>
              /// Load an image from a file from application resource.
              /// </summary>
              /// <returns>The new TaskParameter.</returns>
              /// <param name="resourceName">Name of the resource in drawable folder without extension</param>
              TaskParameter LoadCompiledResource(string resourceName);
    

    Xamarin.Android

    xml:

     <FFImageLoading.Views.ImageViewAsync
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/> 
    

    Drawable Resource: with AndroidResource of BuildAction

    enter image description here

    MainActivity:

    var filePath = "sample";
    
    ImageService.Instance.LoadCompiledResource(filePath).WithCustomDataResolver(new SvgDataResolver(64, 0, true)).Into(imageView);
    

    enter image description here

    In the link you provided, it also shows the way to load from embedded resources on Xamarin.Forms, you could check it. https://github.com/luberda-molinet/FFImageLoading/wiki/SVG-support#xamarinandroid