2014-03-28 37 views
1

我需要下載並顯示在細胞圖像和我使用如何提高緩存的到期時間在AFNetworking爲iOS

- (void)setImageWithURL:(NSURL *)url 

方法爲,但一段時間後,下載的圖像從細胞和下載再次消失。我發現圖像只能保存在緩存中30秒,但我想永久保存在緩存中,並希望通過調用釋放緩存的功能手動刪除緩存。可能嗎?

+0

這是可能的。但它需要您將它存儲在手機上,並隨時隨地從其中檢索。順便說一句,考慮到你的問題,你的問題的標題很混亂。您可能想要更改它。 –

+0

但AFNetworking API中是否有任何方法? – Developer

+0

你在表格視圖單元格中設置圖像嗎? – amar

回答

0

NSURLCache檢查服務器的緩存策略的服務器頭以存儲圖像。還要確保您的緩存足夠大以存儲圖像。如果圖像大小很大,那麼5%(或某些東西),那麼高速緩存大小的圖像不被高速緩存。

由於AFNetworking使用默認NSURLCache您最好的選擇是子類NSURLCache。隨着你的自定義版本NSURLCache,你可以檢查請求是否是一個圖像,如果存儲的話,你需要在caches目錄中的某個地方存儲它。

這樣,因爲你是用to./

+0

我認爲AFNetworking已經在做但不知道它在哪裏存儲圖像。看到這個---------------(void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage { NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url]; [request addValue:@「image/*」forHTTPHeaderField:@「Accept」]; ------------ – Developer

+0

所有的'NSURLConnection' /'NSURLSession'都使用'NSURLCachce'。由於AFNetworking構建在NSURLConnection/NSURLSession之上,因此它們使用NSURLCache。因此,如果緩存太小,圖像將被緩存。如果你想讓圖像緩存更長的時間,那麼服務器頭告訴'NSURLCache'它們應該被緩存,你將不得不編寫你自己的'NSURLCache'版本。 – rckoenes

0

一個好方法,當你與AFNetworking檢索它使用以下方法來永久處理文件,你可以繼續使用AFNetworking

//Store the file 
+ (void) cacheFile:(NSString *)fileName data:(NSData *)data 
{ 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fileName]; //add our image to the path 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    BOOL ok = [fileManager createFileAtPath:fullPath contents:data attributes:nil]; 

    if (!ok) { 
     NSLog(@"Error creating file %@", fullPath); 
    } else { 
     NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:fullPath]; 
     [myFileHandle writeData:data]; 
     [myFileHandle closeFile]; 
    } 
} 

//Check if file is already stored 
+ (BOOL) fileExistsInCache:(NSString *)fileName 
{ 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 

    DLog(@"path to file's existance, %@", fullPath); 

    return [[NSFileManager defaultManager] fileExistsAtPath:fullPath]; 
} 

//Load file from storage 
+ (NSData *) loadCachedFile:(NSString *)fileName 
{ 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 

    DLog(@"path to file's existance, %@", fullPath); 

    NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:fullPath]; 
    return [myFileHandle readDataToEndOfFile]; 
} 

//Remove file from storage 
+ (void) removeCachedFile:(NSString *)fileName 
{ 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fileName]; 

    BOOL ok = [[NSFileManager defaultManager] removeItemAtPath:fullPath error:NULL]; 
} 

//Find out what is already stored 
+ (NSMutableArray *)returnAllCachedMediaFiles 
{ 
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSFileManager *fm = [NSFileManager defaultManager]; 
    NSArray *dirContents = [fm contentsOfDirectoryAtPath:documentsDirectory error:nil]; 

    DLog(@"dirContents %@", dirContents); 

    NSMutableArray *cachedMediaFiles = [[[NSMutableArray alloc] init] autorelease]; 

    for (NSString *fileName in dirContents) 
    { 
     if ([fileName hasSuffix:@".png"] || [fileName hasSuffix:@".jpg"]) 
     { 
      [cachedMediaFiles addObject:fileName]; 
     } 
    } 

    return cachedMediaFiles; 
} 

的您的項目中的正確展示位置我不能說。但是,這些方法將爲您提供足夠的手柄來處理。 請記住,使用documentsDirectory,你可能想使用自己的東西(如NSCachesDirectory或其他)。

0

這裏簡單的方法來獲取圖像

- (void) cacheImage: (NSString *) ImageURLString 
{ 
assert(nil != ImageURLString); 

NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
NSString *myPath = [myPathList objectAtIndex:0]; 
NSString * fileName = [ImageURLString lastPathComponent]; 
NSString * uniquePath = [myPath stringByAppendingPathComponent:fileName]; 
// Check for file existence 
if(![[NSFileManager defaultManager] fileExistsAtPath: uniquePath]) 
{ 
    // Fetch image from url and cache it 
    NSData *data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:ImageURLString]]; 
    [data writeToFile:uniquePath atomically:YES]; 
} 

} 

- (UIImage *) getCachedImage: (NSString *) ImageURLString 
{ 
assert(nil != ImageURLString); 

// create unique path for image 
NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
NSString *myPath = [myPathList objectAtIndex:0]; 
NSString * fileName = [ImageURLString lastPathComponent]; 
NSString * uniquePath = [myPath stringByAppendingPathComponent:fileName]; 

UIImage *image; 

// Check for a cached version 
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath]) 
{ 
    image = [UIImage imageWithContentsOfFile: uniquePath]; //this is the cached image 
} 
else 
{ 
    // cache image and return cached image 
    [self cacheImage: ImageURLString]; 
    image = [UIImage imageWithContentsOfFile: uniquePath]; 
} 

return image; 
} 

通過調用第二個方法就可以獲取緩存圖像,如果沒有緩存它會從URL獲取並存儲在文件中,並返回圖像你。我沒有實施清潔代碼。

相關問題