2017-05-05 76 views
-1

我使用SDWebImage使用密鑰存儲在緩存和磁盤存儲器中的影像,但同時使用該密鑰我沒有得到的圖像查詢,代碼SDImageCache無法檢索緩存中保存的圖像

SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"ImageCacheFolder"]; 
[imageCache queryCacheOperationForKey:[entryidarray objectAtIndex:indexpath.row] done:^(UIImage *image, NSData *data, SDImageCacheType cacheType) { 
    if (image) { 
     NSLog(@"image get from cache"); 
     [imageview setImage:image]; 
    } else { 
     [imageview sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"PlaceholderImage-Day"] options:SDWebImageProgressiveDownload]; 
    } 
}]; 

NSURL *url = [NSURL URLWithString:imgStr]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
UIImage *img = [[UIImage alloc] initWithData:data]; 

if (img) { 
    NSLog(@"the image is not nil"); 
    [[SDImageCache sharedImageCache] storeImage:img forKey:[entryidarray objectAtIndex:indexpath.row] toDisk:YES completion:nil]; 
}else { 
    NSLog(@"the image is nil"); 
} 
+0

默認'SDImageCache'使用的緩存策略,那麼爲什麼你需要額外的緩存 –

+0

一個特點是那裏,我需要使用爲什麼我試圖像這就是關鍵,以獲得圖像。 –

+1

好吧,那麼爲什麼你使用兩個線程 –

回答

2

試試這個 。

if (img) { 
    NSLog(@"the image is not nil"); 
    [imageCache storeImage:img forKey:[entryidarray objectAtIndex:indexpath.row] toDisk:YES completion:nil]; 
}else { 
    NSLog(@"the image is nil"); 
} 
+0

它的作品KKRocks,非常感謝你:-) –