Xamarin forms: Image Cache
Asked Answered
J

1

5

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

Justifier answered 5/1, 2017 at 14:35 Comment(3)
Are your images named dynamically or do they have a static url such as mywebsite.com/myimage.png?Ransom
@user1 dynamically namedJustifier
web images are cached by default for 24 hours with Xamarin.Forms.Image. You can increase that by changing CacheValidityLivonia
R
9

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.

Ransom answered 5/1, 2017 at 15:24 Comment(7)
But how will I check it if it is in cache or notJustifier
I want my images to download only if they are not in app cacheJustifier
Xamarin Forms Image will do that check for you, and if not redownload the imageRansom
My app can run in offline mode also that is why I am looking for image cache. Will it make any difference if no internet is there?Justifier
If the application cannot download the image to the cache then no image will be displayed. Once the image is in the cache I suggest setting the CacheValidity to Timspan.MaxValue then the image will be cached foreverRansom
Then how my default image will come up then? I first need to set my local default image and then URI Image source?Justifier
Is there any callback function that I can use for getting the image download complete event? I need to catch image download event?Justifier

© 2022 - 2024 — McMap. All rights reserved.