Ios 图像缓存表

Ios 图像缓存表,ios,iphone,objective-c,caching,Ios,Iphone,Objective C,Caching,我希望能够脱机查看我的应用程序和表中的所有图像。目前我所有的图片都被缓存了。但不是实际的结果表 NSMutableArray *images; - (void)viewDidLoad { NSURL *url = [NSURL URLWithString:@"http://xxxxxxx.co.uk/jsonimages.php"]; NSData *jsonData = [NSData dataWithContentsOfURL:url]; NSError *e

我希望能够脱机查看我的应用程序和表中的所有图像。目前我所有的图片都被缓存了。但不是实际的结果表

NSMutableArray *images;
- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:@"http://xxxxxxx.co.uk/jsonimages.php"];
        NSData *jsonData = [NSData dataWithContentsOfURL:url];
        NSError *error = nil;

        if (jsonData) {

            NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                   options:NSJSONReadingMutableContainers error:&error];
            images = result[@"images"];
        }
}
这给了我一个图像URL列表,然后我在我的表上有如下内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:encodedString]
                            placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
}
它使用SDWebimage缓存
encodedString


但如果我在没有任何互联网接入的情况下使用该应用程序,则不会出现任何图像。这是因为我需要缓存阵列吗?

我建议使用core data来存储驱动表视图的信息,您也可以将图像下载到documents目录,并在core data中存储对其在磁盘上位置的引用,看一看Ray Wenderlich的一些很棒的教程

在没有互联网的情况下使用应用程序时,你看不到这些图像,因为你的应用程序使用的是内存缓存。如果您希望在退出应用程序时图像仍然存在,则应使用磁盘缓存。您可以配置
SDWebimage
库来处理此问题

SDWebimage
文档:

也可以使用基于aync的图像缓存存储 独立地。SDImageCache维护一个内存缓存和一个可选的 磁盘缓存。磁盘缓存写操作是异步执行的,因此 它不会给UI增加不必要的延迟

编辑: 您可以这样做(代码未经测试):


只需使用惊人的DLImageLoader。完整的代码示例。。嘿,谢谢分享这个。我将如何用我的代码实现这一点?我不太明白我应该看的文档是什么?什么是
图像
?我得到了这个错误:
不兼容的块指针类型将'void(^)(UIImage*\uuuuu strong)'发送到'SDWebImageQueryCompletedBlock'类型的参数(也称为'void(^)(UIImage*\uu strong,SDImageCacheType)
啊,是的。SDWebImage的维护者没有更新文档以包含新方法queryDiskCacheForKey:…我已经更新了我的答案。它现在根本没有下载图像!
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    SDImageCache *imageCache = [SDImageCache sharedImageCache];
    [imageCache queryDiskCacheForKey:encodedString done:^(UIImage *image, SDImageCacheType cacheType) {

 if (image){
             cell.thumbnailImageView.image = image;
         }else{
 [cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:encodedString] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            [imageCache storeImage:image forKey:encodedString];

        }];  
}


    }];

return cell;
}