使用NSURLRequest和NSURLConnection进行磁盘缓存是否在iPhone上实际工作? [英] Does disk caching with NSURLRequest and NSURLConnection actually work on the iPhone?

查看:70
本文介绍了使用NSURLRequest和NSURLConnection进行磁盘缓存是否在iPhone上实际工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,其单元格包含自定义ImageView,这些View异步地从Internet加载图像。为了加载这些图像,我使用了NSURLRequest和NSURLConnection,它们工作正常。唯一的问题是图像不会被缓存,因此每次使用时都会下载。我试图将NSURLRequest的cachePolicy设置为 NSURLRequestReturnCacheDataElseLoad,但没有任何效果。

I have a UITableView whose cells contain custom ImageViews that asynchronously load the images from the internet. To load those images I use NSURLRequest and NSURLConnection which works fine. The only problem is that the images are not cached and therefore are downloaded every time they are used. I tried to set the cachePolicy of the NSURLRequest to "NSURLRequestReturnCacheDataElseLoad" with no effect.

从最后一个答案看来链接,iPhone上的NSURLRequest不支持磁盘缓存。这是正确的吗?

It seems from the last answer on link that disk caching is not supported with NSURLRequest on the iPhone. Is this correct?

如果它确实可以工作,我想知道在我的情况下它不起作用的原因可能是什么。

If it should actually work, I'd be interested to know what could be the reason that it doesn't work in my case.

代码如下:

NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://myurl"] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];


connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


推荐答案

我认为应该可以。如果不是,则可能意味着Web服务器实际上正在提供不可缓存的内容。例如,因为它没有设置正确的标题。

I think it should work. If it does not then it could mean that the web server is actually serving content that is not cacheable. For example because it is not setting the right headers.

如果您可以将链接发布到其中一张图像,那么我们可以仔细看一下。

If you can post a link to one of the images then we can take a close look.

HTTP/1.1 200 OK
Date: Mon, 01 Mar 2010 12:40:46 GMT
Server: Apache
Last-Modified: Mon, 01 Mar 2010 09:33:29 GMT
ETag: "25e8dd2-11a6-480b9f1178c40"
Accept-Ranges: bytes
Content-Length: 4518
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: image/png

这是您的一张图片作为标题返回的内容。我没有看到 Expires Cache-Control 标头,所以这可能就是为什么不缓存它们的原因。我认为就WebKit而言,这是动态内容,每次都应重新加载。

This is what one of your images returns as headers. I don't see a Expires or Cache-Control header, so that may be why they are not cached. I think as far as WebKit is concerned, this is dynamic content that should be reloaded every time.

这篇关于使用NSURLRequest和NSURLConnection进行磁盘缓存是否在iPhone上实际工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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