代码摘自:SDWebImage
- (void)start
{
// In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
// Ensure we aren't blocked by UI manipulations (default runloop mode for NSURLConnection is NSEventTrackingRunLoopMode)
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[connection start];
[request release];
if (connection)
{
self.imageData = [NSMutableData data];
[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:nil];
}
else
{
if ([delegate respondsToSelector:@selector(imageDownloader:didFailWithError:)])
{
[delegate performSelector:@selector(imageDownloader:didFailWithError:) withObject:self withObject:nil];
}
}
}
1
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
1
1
1
1
1
NSURLRequestCachePolicy
缓存策略
原文地址:
点击打开链接
1> NSURLRequestUseProtocolC
achePolicy = 0, 默认的缓存策略,如果缓存不存在,直接从服务端获取。如果缓存存在,会根据response中的Cache-Control字段判断下一步操作,如:Cache-Control字段为must-revalidata,则询问服务端该数据是否有更新,无更新的话直接返回给用户缓存数据,若已更新,则请求服务端.
2> NSURLRequestReloadIgnori
ngLocalCacheData = 1,忽略本地缓存数据,直接请求服务端.
3> NSURLRequestIgnoringLoca
lAndRemoteCacheData = 4,忽略本地缓存,代理服务器以及其他中介,直接请求源服务端.
4> NSURLRequestReloadIgnori
ngCacheData =NSURLRequestReloadIgnori
ngLocalCacheData
5> NSURLRequestReturnCacheD
ataElseLoad= 2,
有缓存就使用,不管其有效性(
即忽略Cache-Control
字段),
无则请求服务端.
6> NSURLRequ