Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

ios - Cache Images in tableviewcontroller

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;
                                               }];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...