在 tableviewcontroller 中缓存图像

问题描述:

我想将图像保存到 TableView 控制器的缓存中.我写了以下代码,但 cacheImage 始终为零.

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

我按照 Joe 的建议使用了以下代码,但 cell.imageview.image 始终为零.

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

不要试图重新发明*.你应该使用外部库来做到这一点,有一些很好的例子.看看 SDWebImage,它完全符合您的要求.

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>

并且,在您的 tableView:cellForRowAtIndexPath: 方法中,您可以使用 setImageWithURL:(或任何其变体:带有占位符等)库提供的方法:

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

仅此而已.图书馆会为您处理一切.如果你想以某种方式管理它背后的缓存并配置它,你可以使用 SDWebImageManager 类(GitHub 页面中的更多信息).

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