2015-04-02 35 views
0

我是AFNetworking的新手,在iOS開發方面經驗不足。緩存使用AFNetworking的大圖

我用AFNetworking從互聯網上下載圖像,這裏是代碼:

- (void)dowloadPoject { 

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad; 

BOOL __block responseFromCache = YES; // yes by default 

void (^requestSuccessBlock)(AFHTTPRequestOperation *operation, id responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject) { 
    if (responseFromCache) { 
     // response was returned from cache 
     NSLog(@"RESPONSE FROM CACHE: %@", responseObject); 
    } 
    else { 
     // response was returned from the server, not from cache 
     NSLog(@"RESPONSE From Server: %@", responseObject); 
    } 

    UIImage *downloadedImage = [[UIImage alloc] init]; 
    downloadedImage = responseObject; 

    // Add & Reload Data 
    [self.projectImage addObject:downloadedImage]; 
    [self.projectname addObject:@"ABC"]; 
    [self reloadComingSoonProject]; 
    [self reloadNewReleaseProject]; 
}; 

void (^requestFailureBlock)(AFHTTPRequestOperation *operation, NSError *error) = ^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"ERROR: %@", error); 
}; 

AFHTTPRequestOperation *operation = [manager GET:@"http://i359.photobucket.com/albums/oo34/SenaSLA/walls/Forward-Arrow-Button.png" 
             parameters:nil 
             success:requestSuccessBlock 
             failure:requestFailureBlock]; 
operation.responseSerializer = [AFImageResponseSerializer serializer]; 

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 
    NSLog(@"bytesRead: %u, totalBytesRead: %lld, totalBytesExpectedToRead: %lld", bytesRead, totalBytesRead, totalBytesExpectedToRead); 

    [self.viewNavBar.progressbar setProgress:(totalBytesRead/totalBytesExpectedToRead) animated:YES]; 
}]; 

[operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) { 
    // this will be called whenever server returns status code 200, not 304 
    responseFromCache = NO; 
    return cachedResponse; 
}]; 
} 

我一直在尋找互聯網上,我仍然沒有得到正確的解決方案。我已經嘗試了rckoenes這個解決方案,但是這對我沒有任何作用。

我已經成功緩存像4kb的圖像,但是當我嘗試像103kb的圖像它不緩存。謝謝。

+0

不相關,但是你的代碼說'UIImage * downloadedImage = [[UIImage alloc] init]; downloadedImage = responseObject;'可以簡化爲'UIImage * downloadedImage = responseObject;'。 – Rob 2015-04-02 05:19:16

+0

好的,謝謝。 – 2015-04-02 06:29:27

回答

1

您可以嘗試增加緩存大小。上次我測試這個時,如果接收到的數據超過總高速緩存大小的5%,網絡代碼將不會緩存(儘管令人煩惱的是,我還沒有看到Apple清楚地說明緩存過程中應用的規則)。

無論如何,如果你看一下樣品AFNetworking項目應用程序的委託,那就說明和舉例如何指定緩存大小:

NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil]; 
[NSURLCache setSharedURLCache:cache]; 

它看起來像默認的RAM緩存只有0.5MB和磁盤緩存爲10MB。通過將RAM緩存提高到4MB(如上圖所示)或更大,並且您應該可以緩存您的103kb圖像(假設所有其他標準(例如響應的標頭字段等)都允許)。

+0

Hi @Rob。經過幾次谷歌搜索的圖像測試。我發現我的程序可以緩存圖像甚至高達2.5MB,來源是從一個網站(我谷歌搜索)。但我無法緩存從Dropbox下載的圖像(這是圖像103KB圖像)。你知道爲什麼嗎? – 2015-04-02 06:33:15

+0

應用了許多標準。大小(以高速緩存大小的百分比)以及標題。比較回覆的標題,我懷疑你會看到不同之處。坦率地說,你可能會考慮使用你自己的應用級緩存。例如,我相信這就是AFNetworking的'UIImageView'類別。 – Rob 2015-04-02 16:27:41