2014-05-24 134 views
0

我希望能够离线查看我的应用程序以及我的表中的所有图像。目前我所有的图像都被缓存。但不是实际的结果表格。图像缓存表

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

所以,让我像一个网址列表,然后我有我的表是这样的:

- (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"]]; 
} 

用于缓存encodedString使用SDWebimage

但是,如果我去到应用程序没有任何访问互联网的图像没有出现。这是因为我需要缓存数组?

+0

只需使用** ** AMAZING DLImageLoader。完整的代码示例.. http://stackoverflow.com/a/19115912/294884 – Fattie

回答

2

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

SDWebimage文档:

It is also possible to use the aync based image cache store independently. SDImageCache maintains a memory cache and an optional disk cache. Disk cache write operations are performed asynchronous so it doesn't add unnecessary latency to the UI.

Complete documentattion

编辑: 你可以做这样的事情(代码未测试):

- (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; 
} 
+0

嘿谢谢分享这个。我将如何使用我的代码实现这一点?我不太明白文档是[this](https://github.com/rs/SDWebImage#using-asynchronous-image-caching-independently)我应该看的那个? – maxisme

+0

看我的编辑。代码未经测试 – samir

+0

什么是“图像”? (')(UIImage * __ strong,SDImageCacheType)')' – maxisme

0

我会建议使用核心数据来存储驱动您的表视图的信息,您也可以将图像下载到文档目录并在核心数据中存储磁盘位置的引用,查看herehere Ray Wenderlich提供的一些很棒的教程