本文介绍了通用图像装载机:我可以使用缓存,但也刷新了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载动态生成的图像,所以我总是希望他们是最新的。但是,他们需要时间来加载,所以我也想显示缓存版本,而更新一个不来的。我怎样才能做到这一点与通用图像装载机?

更具体地讲,当我打电话displayImage我希望它做到以下几点:

  1. 如果缓存的图像是否显示它的时候了。
  2. 开始从给定的URL反正下载。
  3. 当图片加载完成后,在视图中显示它取代了缓存的形象。
  4. 更新缓存。
解决方案

所以,最后我用了一个ImageLoadingListener如下:

onLoadingStarted:加载启动时检查缓存。

onLoadingComplete:如果没有找到缓存,然后什么也不做。的请求将被发送到网络和高速缓存将被自然地更新。否则,清除缓存,并再次呼吁displayImage(没有监听这需要时间)。缓存的图像将显示在通常的观点来。此外,当第二装载完成时,图和高速缓冲存储器将被更新。

  ImageLoader.getInstance()。displayImage(imageUri,看来,新SimpleImageLoadingListener(){
            布尔cacheFound;

            @覆盖
            公共无效onLoadingStarted(字符串URL,浏览视图){
                名单<字符串> MEMCACHE = MemoryCacheUtils.findCacheKeysForImageUri(URL,ImageLoader.getInstance()getMemoryCache());
                cacheFound = memCache.isEmpty()!;
                如果(!cacheFound){
                    文件discCache = DiscCacheUtils.findInCache(URL,ImageLoader.getInstance()getDiscCache());
                    如果(discCache!= NULL){
                        cacheFound = discCache.exists();
                    }
                }
            }

            @覆盖
            公共无效onLoadingComplete(字符串imageUri,查看视图,位图loadedImage){
                如果(cacheFound){
                    MemoryCacheUtils.removeFromCache(imageUri,ImageLoader.getInstance()getMemoryCache());
                    DiscCacheUtils.removeFromCache(imageUri,ImageLoader.getInstance()getDiscCache());
                    。ImageLoader.getInstance()displayImage(imageUri,(ImageView的)观点);
                }
            }
        });
    }
 

I'm loading dynamically generated images so I always want them to be up to date. But they take time to load so I also want to display a cached version while the updated one doesn't come.How can I do this with Universal Image Loader?

More specifically, when I call "displayImage" I want it to do the following:

  1. If a cached image exists display it right away.
  2. Start downloading from the given url anyways.
  3. When the image loading finishes, display it in the view replacing the cached image.
  4. Update the cache.
解决方案

So in the end I used an ImageLoadingListener as follows:

onLoadingStarted:Check for cache when loading starts.

onLoadingComplete:If no cache was found then do nothing. The request will be sent to network and cache will be updated naturally.Otherwise clear cache and call displayImage again (no listener needed this time). The cached image will be shown in the view normally. Moreover, when the 2nd loading finishes, view and cache will be updated.

ImageLoader.getInstance().displayImage(imageUri, view, new SimpleImageLoadingListener() {
            boolean cacheFound;

            @Override
            public void onLoadingStarted(String url, View view) {
                List<String> memCache = MemoryCacheUtils.findCacheKeysForImageUri(url, ImageLoader.getInstance().getMemoryCache());
                cacheFound = !memCache.isEmpty();
                if (!cacheFound) {
                    File discCache = DiscCacheUtils.findInCache(url, ImageLoader.getInstance().getDiscCache());
                    if (discCache != null) {
                        cacheFound = discCache.exists();
                    }
                }
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if (cacheFound) {
                    MemoryCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getMemoryCache());
                    DiscCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getDiscCache());
                    ImageLoader.getInstance().displayImage(imageUri, (ImageView) view);
                }
            }
        });
    }

这篇关于通用图像装载机:我可以使用缓存,但也刷新了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:59