Search code examples
c#xamarin.formsportable-class-librarymemorycache

Xamarin forms: Image Cache


Hi I am trying to build an app using xamarin forms PCL. I am implementing image gallery in which I have used a default image. All the images are on blob. I want to download image and cache that Image in device and as soon as download is complete I need to replace my default image with it. And on loading app next time download image only if it is not present in cache. I dont get any plugin for image caching and loading image from cache. I have seen a plugin named FFPLUGIN but it didnt work. Any idea how I can implement this? IMAGE CACHING


Solution

  • You could use the built in ImageCaching in Xamarin Forms shown here:

    https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Downloaded_Image_Caching

    Downloaded Image Caching

    UriImageSource also supports caching of downloaded images, configured through the following properties:

    CachingEnabled - Whether caching is enabled ( true by default).

    CacheValidity - A TimeSpan that defines how long the image will be stored locally. Caching is enabled by default and will store the image locally for 24 hours. To disable caching for a particular image, instantiate the image source like this:

    Image.Source = new UriImageSource {CachingEnabled = false,
    Uri="http://server.com/image"}; To set a specific cache period (for
    example, 5 days) instantiate the image source like this:
    
    
    webImage.Source = new UriImageSource {
    Uri = new Uri("https://xamarin.com/content/images/pages/forms/example-app.png"),
    CachingEnabled = true,
    CacheValidity = new TimeSpan(5,0,0,0) };
    

    Built-in caching makes it very easy to support scenarios like scrolling lists of images, where you can set (or bind) an image in each cell and let the built-in cache take care of re-loading the image when the cell is scrolled back into view.