【问题标题】:Xamarin - how to reset image cache for a specific imageXamarin - 如何重置特定图像的图像缓存
【发布时间】:2019-03-22 16:27:32
【问题描述】:

在我的 Xamarin 应用程序中,我在服务器上上传了一个新的“用户配置文件”图像,然后我尝试使用新版本的图像更新图像(注意到图像 uri 仍然相同)。

Xamarin 提供了一个图像缓存,我需要使其无效以强制从服务器重新加载图像。但似乎无法使图像缓存失效!我找不到解决办法!

我尝试了几种解决方案,但都没有办法!即使重新启动应用程序,我也会得到旧版本的图像。

我尝试过一些类似的东西:

(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);

但没有任何工作,欢迎任何想法?谢谢

【问题讨论】:

    标签: image xamarin xamarin.forms ffimageloading


    【解决方案1】:

    我执行以下操作:

    // 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(...);
    

    这会重新加载图像,而 FFImageLoading 甚至会在图像发生变化时做出漂亮的淡入淡出动画。

    【讨论】:

      【解决方案2】:

      CachedImage 中其实有一个静态方法可以接受ImageSource 的任何孩子,所以不用猜关键:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-17
        • 1970-01-01
        • 2013-08-16
        • 2015-12-18
        • 1970-01-01
        • 2015-03-25
        • 2013-12-19
        相关资源
        最近更新 更多