SDImageCache无法在Cache中检索已保存的图像

时间:2017-05-05 12:25:47

标签: ios objective-c caching sdwebimage

我使用SDWebImage使用密钥将图像存储在缓存和磁盘存储器中,但在使用该密钥查询时我没有得到图像,代码是

 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");
}

1 个答案:

答案 0 :(得分: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");
}