UIImageView缓存策略-AFNewtorking [英] UIImageView Cache policies - AFNewtorking

查看:76
本文介绍了UIImageView缓存策略-AFNewtorking的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有多个UIImageView,它们使用AFNetworking的 setImageWithURL 填充服务器中的图像。该文档说,此方法使用标准的UIImageView缓存策略。我的问题是,如果我更新服务器上的映像,则该映像不会足够快地在应用程序中更新。即使我退出应用程序并重新启动。如果要更新图像,则必须重新安装该应用程序,这会清除缓存并引入新图像。显然,这对用户而言并不是理想的效果。

I have several UIImageView's in my app that I use AFNetworking's setImageWithURL to populate an image from my server. The documentation says that this method uses the standard UIImageView cache policy. My issue is that if I update an image on my server, the image does not get updated in the app soon enough. Even if I close out of the app and restart. If I want images to update I have to reinstall the app, which clears the cache and brings in new images. Obviously this is not a desired effect for the user.

AFNetworking的缓存是私有的,因此在UIImageView + AFNetworking类别中无法直接访问它。关于清除整个应用程序缓存有什么想法?我将如何实现?

AFNetworking's cache is private, so I don't have direct access to it in the UIImageView + AFNetworking category. Any thoughts on clearing the whole app cache? How would I achieve that?

推荐答案

您可以访问 +(AFImageCache *)af_sharedImageCache 方法,最简单的方法是将其复制到接口文件 UIImageView + AFNetworking.h 中。这将公开单例NSCache子类。然后,您可以调用 – removeObjectForKey: – removeAllObjects 分别删除图像或所有图像。

You can access the + (AFImageCache *)af_sharedImageCache method, most easily by copying it into the interface file UIImageView+AFNetworking.h. This will expose the singleton NSCache subclass. Then you can call – removeObjectForKey: or – removeAllObjects to remove the image or all images, respectively.

如果您不想这样做,请考虑使用 NSHTTPURLResponse 对象作为键来缓存图像。您可以通过向URL附加一些随机值来创建其他键(例如,可以附加?garbage = 4H5G789H35G89H )。但是,这基本上会浪费内存,因为您将缓存永远无法检索的图像。

If you don't want to do this, consider that the image is cached using the NSHTTPURLResponse object as the key. You could create a different key by appending some random value to the URL (for example, you could append ?garbage=4H5G789H35G89H). However, this would basically waste memory as you'd be caching images that would never be retrieved.

最后,您可能要提交增强请求(或自己编写一个代码)以公开缓存或使缓存可选。

Finally, you may want to submit an enhancement request (or code one yourself) to expose the cache or make caching optional.

这篇关于UIImageView缓存策略-AFNewtorking的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆