0

I am wanting to be able to view my app offline and all the images in my table. Currently all my images get cached. But not the actual table of results.

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

So that gives me a list of image urls, which I then have on my table like this:

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

Which caches encodedString using SDWebimage

But if I go onto the app without any access to the internet no images appear. Is this because I need to cache the array?

maxisme
  • 3,974
  • 9
  • 47
  • 97
  • Simply use the **AMAZING** DLImageLoader. Full code examples .. http://stackoverflow.com/a/19115912/294884 – Fattie May 24 '14 at 11:59

2 Answers2

2

You don't see the images when lunching your app without internet because your are using a memory cache. You should use a disk cache if you want your images still there when you quit your application. You can configure the SDWebimage library to handle this for you.

From the SDWebimage documentation :

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

Edit : You can do something like this (code not tested) :

  - (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;
}
samir
  • 4,501
  • 6
  • 49
  • 76
  • Hey thanks for sharing this. How would I implement this with my code? I don't quite understand the documentation is [this](https://github.com/rs/SDWebImage#using-asynchronous-image-caching-independently) the one I should be looking at? – maxisme May 24 '14 at 16:23
  • What is the `image` ? – maxisme May 24 '14 at 16:38
  • I am getting this error: `Incompatible block pointer types sending 'void (^)(UIImage *__strong)' to parameter of type 'SDWebImageQueryCompletedBlock' (aka 'void (^)(UIImage *__strong, SDImageCacheType)')` – maxisme May 24 '14 at 16:39
  • ah yes. The maintainer of the SDWebImage does'nt update the documentation to include the new method queryDiskCacheForKey:...i have updated my answer. – samir May 24 '14 at 17:00
  • It is not downloading the images at all now! – maxisme May 24 '14 at 17:06
  • I have put my iphone on flight mode after 'opening' all the images and they still don't appear – maxisme May 24 '14 at 17:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54352/discussion-between-maximilian-and-samir). – maxisme May 24 '14 at 17:31
0

I would suggest using core data to store the information which drives your table view, you could also download the images to the documents directory and store a reference to their location on disk in core data, take a look here and here for some great tutorials by Ray Wenderlich

Drew
  • 121
  • 1
  • 7