2014-02-16 125 views
0

我下載圖像並在我的應用程序中使用它。我有超過100張圖片,並且我一次不會全部使用它。我將使用緩存。然後我加載所有保存到緩存的圖像。然後我會去應用程序的其他部分,我會使用一些圖像。緩存策略,AFNetworking

我不是很瞭解ios緩存是如何工作的,我有一個問題:在加載所有圖像並將其保存到緩存後,我應該如何使用這些圖像,我的意思是,我需要從緩存中加載它們或使用圖像實例我有保存之前ti緩存?

什麼樣的緩存策略對AFNetworking 2.0更有用?

回答

0

AFNetworking具有內存緩存圖像。如果你有100張圖片,它可能適合你,可能不適合。

您還應該在這裏閱讀有關NSURLCache的信息 - http://nshipster.com/nsurlcache/

如果你使用NSURLCache,你不必考慮緩存 - 當你開始下載已經緩存的東西時,系統會給你下載的文件。

更新: 下載數據的時間到期由服務器響應設置。但是你可以通過NSURLConnectionDelegate方法編輯或忽略它。例如:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { 

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)[cachedResponse response]; 

    // Look up the cache policy used in our request 
    if([connection currentRequest].cachePolicy == NSURLRequestUseProtocolCachePolicy) { 
     NSDictionary *headers = [httpResponse allHeaderFields]; 
     NSString *cacheControl = [headers valueForKey:@"Cache-Control"]; 
     NSString *expires = [headers valueForKey:@"Expires"]; 
     if((cacheControl == nil) && (expires == nil)) { 
      NSLog(@"server does not provide expiration information and we are using NSURLRequestUseProtocolCachePolicy"); 
      return nil; // don't cache this 
     } 
    } 
    return cachedResponse; 
} 
+0

我可以使用NSURLCache和AFNetworking Image響應嗎? – user3300062

+0

是的。你可以。 – Avt

+0

好的,這很好,最後一個問題,直到我不能接受你的答案,我如何設置時間來存儲緩存? – user3300062