AFNetworking的UIImageView类别的预缓存映像 [英] Pre-cache Images for AFNetworking's UIImageView category

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

问题描述

当我的应用加载时,我下拉了99个对象的JSON表示。

When my app loads, I pull down a JSON representation of 99 objects.

每个对象都有一个'image_url'字段,我将其传递给AFNetworking的 setImageWithURLRequest

Each object has an 'image_url' field, which I pass to AFNetworking's setImageWithURLRequest.

我的图片加载到tableView中,因此,只有前几个单元格对其图像进行请求。直到我向下滚动才会发出后续图像请求。

My images load in a tableView, and consequently, only the first several cells make requests for their images. It's not until I scroll down that subsequent image requests are made.

一旦我下拉初始数据集,我希望能够启动后台进程熄灭并下载最初不可见的95个左右的对象,并以这样的方式缓存它们:当调用 setImageWithURLRequest 时,它已经有一个缓存的图像来自。

Once I pull down the initial dataset, I'd like to be able to kick off a background process that goes out and downloads the 95 or so objects that aren't initially visible, and cache them in such a way that when setImageWithURLRequest is called, it'll already have a cached image to pull from.

虽然AFImageCache是​​私有的,所以我不确定这是否可行。我知道我可以使用NSURLCache进行缓存,但之后我会有两个独立的,孤立的缓存,这也不理想。

AFImageCache is private though, so I'm not sure if this is possible. I know I could cache with NSURLCache, but then I'd have two separate, isolated caches, and that's not ideal either.

我唯一的选择是不使用AFNetworking的UIImageView category?

Is my only option to not use AFNetworking's UIImageView category?

这些答案让我这么认为:

These answers make me think so:

iOS使用AFImageCache缓存图像似乎不起作用

如何在使用AFNetworking的setImageWithURL时配置缓存

推荐答案

不要这样做。

Please, please don't do this.

当我说这几乎肯定是不必要的时候相信我。

Trust me when I say that this almost certainly unnecessary.

事实上,由于下载可能永远不会被查看的图像的压力增大,它可能会产生与预期效果相反的效果。

In fact, it will likely have the opposite of the desired effect, due to the increased pressure of downloading images that will probably never be viewed.

缓存是私有的,原因很简单 - 只是为了加快滚动视图上的后续请求。只需让表视图按要求下载图像,你就可以了。如果有的话,您可以优化正在下载的图像的大小(确保正确的图像尺寸;智能压缩)。

The cache is private for a very good reason--it's just there to speed up subsequent requests on scroll views. Just have the table view download the images as requested, and you should be just fine. If anything, you can optimize the size of the images that you are downloading (ensure correct image dimensions; compress intelligently).

这篇关于AFNetworking的UIImageView类别的预缓存映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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