Reputation: 4245
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?
Upvotes: 0
Views: 264
Reputation: 4551
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.
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;
}
Upvotes: 2
Reputation: 121
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
Upvotes: 0