Xamarin.Forms caches images

I am using Xamarin.forms to create an app that runs on IOS, Windows Phone and Android. I need to be able to cache images on the device when there is no network available.

The basics:

I am using MVVM approach and have a MenuViewModel with IconImageId property that has GUIDs for different images.

The icons in my view cell are bound to this property:

var icon = new Image
{
    Aspect = Aspect.AspectFit,
    HeightRequest = 80, 
    WidthRequest = 80       
};
icon.SetBinding(Image.SourceProperty, new Binding("IconImageId",BindingMode.Default, new ImageIdToUriImageSourceValueConverter(80, 80, iconColor)));

      

Then I use a custom value converter called ImageIdToUriImageSourceValueConverter to convert the image id to an image:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var applicationUrl = App.ApplicationUrl;
    if (string.IsNullOrEmpty(value as string))
    {
        return null;
    }

    var includeColorOld = string.IsNullOrEmpty(_colorOld) ? "" : string.Format("/{0}", _colorOld);

    // I have an image api which which handles the image itself and that works fine.
    var uriString = string.Format("{0}/img/{1}/{2}{3}/{4}/{5}/{6}", applicationUrl, _x, _y, includeColorOld, _color, value, "image.png");

    return = new UriImageSource
    {
        Uri = new Uri(uriString),
        CachingEnabled = true,
        CacheValidity = new TimeSpan(30, 0, 0, 0)
    };      
}

      

Our image handler uses a GUID to fetch the svg image from our api, resize and re-color it returns, in the requested format for example. PNG. It is designed for image caching and works very well on our websites.

My problem is that images are not being cached on mobile devices or on emulators. Any ideas on what I can do to solve these problems?

Thank,

Loan

+3


source to share


1 answer


You can try https://github.com/molinch/FFImageLoading which is an image replacement (Android, iOS, Windows) with better caching capabilities.



+5


source







All Articles