0

I would like to save images into the cache in TableViewcontroller. I wrote the following code, but cacheImage is always nil.

@property (nonatomic,strong)NSCache *imageCache;
@synthesize imageCache;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageCache = [[NSCache alloc] init];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [[tableData objectAtIndex:indexPath.row] valueForKey:@"name"];
    cell.textLabel.font = [UIFont fontWithName:@"BebasNeue" size:24];
    cell.textLabel.textColor = [UIColor whiteColor];

    UIImage *cachedImage = [imageCache objectForKey:@"indexObject"];

    if (cachedImage) {
          dispatch_async(dispatch_get_main_queue(), ^{
              cell.imageView.image = cachedImage;
                });
         }
    else {


    NSString *imageURLString=[[tableData objectAtIndex:indexPath.row] valueForKey:@"picture"];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *url = [NSURL URLWithString:imageURLString];
        NSData *data = [[NSData alloc] initWithContentsOfURL:url];
        UIImage *tmpImage = [[UIImage alloc] initWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            UITableViewCell *newsCell = [self.grillMenuTable cellForRowAtIndexPath:indexPath];
            [imageCache setObject:tmpImage forKey:@"indexObject"];
            if (newsCell)
            {
                newsCell.imageView.image=tmpImage;
                [newsCell setNeedsLayout];
            }
        });


    });
    }
    return cell;
}

I used the following code as Joe advise, but still cell.imageview.image always nil.

 NSString *imageURLString=[[tableData objectAtIndex:indexPath.row] valueForKey:@"picture"];
    [[DLImageLoader sharedInstance] loadImageFromUrl:imageURLString
                                           completed:^(NSError *error, UIImage *imgData) {
                                               cell.imageView.image = imgData;
                                               }];
  • Are you sure that the image actually loads from this URL? It `tmpImage` in `dispatch_async` initialized? – FreeNickname May 18 '14 at 18:29
  • Yes, I am sure, it loads perfectly fine. But I want cache them to save user data plan and make it much faster. –  May 18 '14 at 18:41

2 Answers2

1

Don't try to reinvent the wheel. You should use an external library to do that, there are some great examples. Take a look at SDWebImage, it does exactly what you want.

Using it, you can forget queues, manually caching... Just import the header:

#import <SDWebImage/UIImageView+WebCache.h>

and, in your tableView:cellForRowAtIndexPath: method, you can just set the image into the UIImageView with the setImageWithURL: (or any of its variants: with placeholders, etc.) method provided by the library:

NSString* imageURL = [[tableData objectAtIndex:indexPath.row] valueForKey:@"picture"];
[cell.imageView setImageWithURL:[NSURL URLWithString:imageURL]];

That's all. The library takes care of everything for you. If you want to somehow manage the cache behind this and configure it, you can, using the SDWebImageManager class (more info in the GitHub page).

sonxurxo
  • 5,648
  • 2
  • 23
  • 33
  • I have implemented the way that you have advised but it does not return the image. here is the code and another question, http://stackoverflow.com/questions/23725663/sdwebimagemanager-image-not-loaded-into-tableviewcontroller –  May 18 '14 at 18:53
  • @user1724168 take a look at my answer here http://stackoverflow.com/questions/23725663/sdwebimagemanager-image-not-loaded-into-tableviewcontroller/23731079#23731079, it's the same I wrote in this question. – sonxurxo May 19 '14 at 06:34
  • @user1724168 have you tried both solutions? If they work, please mark them as correct answers so that others can see it – sonxurxo May 19 '14 at 14:49
1

Here is the tutorial you can follow. Without Using any third party. https://sweettutos.com/2015/12/31/swift-how-to-asynchronously-download-and-cache-images-without-relying-on-third-party-libraries/

Yogendra Singh
  • 2,063
  • 25
  • 20