ClubberLang
ClubberLang

Reputation: 1898

Xamarin - how to reset image cache for a specific image

In my Xamarin app I upload a new "user profile" image on the server, then I try to update the image with the new version of the image (Noticed that the image uri is the still the same).

There is an image cache provided by Xamarin, I need to invalidate it to force the image to reload from the server. But it seems that the Image cache cannot be invalidated ! I find no solution !

I have try several solutions, but find no way ! Even when I restart the application I get the old version of the image.

I have try some stuffs like this :

(MediaImageSource as UriImageSource).CacheValidity = new TimeSpan(-1); FFImageLoading.Forms.CachedImage.InvalidateCache(MediaImageSource, FFImageLoading.Cache.CacheType.All, true); FFImageLoading.ImageService.Instance.InvalidateCacheAsync(FFImageLoading.Cache.CacheType.All);

But nothing work, any idea is welcome ? Thx

Upvotes: 0

Views: 2209

Answers (2)

Matti-Koopa
Matti-Koopa

Reputation: 278

There is actually a static method in CachedImage that accepts any kid of ImageSource, so there is no need to guess the key:

FFImageLoading.Forms.CachedImage.InvalidateCache(myImageSource, CacheType.All, true);

Upvotes: 0

j0ffe
j0ffe

Reputation: 631

I do the following:

// this.Img is the FFImageLoading.CachedImage in my view
var source = this.Img.Source as FileImageSource;

// Reset cache using the global instance (the key is the file name, I assume it is the uri for UriImageSource)
await ImageService.Instance.InvalidateCacheEntryAsync( source.File, CacheType.All, true );

// Reassign the image source (since the binding has not changed per se, the UI may miss the change otherwise)
this.Img.Source = new FileImageSource(...);

This reloads the image and FFImageLoading even does a nice fade animation when it changes.

Upvotes: 2

Related Questions