Cell的图片下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/M_15915899719/article/details/82707334
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *download;
@property (nonatomic, copy) NSString *icon;

+ (instancetype)appWithDict:(NSDictionary *)dict;
+ (instancetype)appWithDict:(NSDictionary *)dict {

    LMApp *app = [[self alloc] init];
    [app setValuesForKeysWithDictionary:dict];
    return app;
}
// 所有的应用数据
@property (nonatomic, strong) NSMutableArray *apps;

// 存放所有下载操作的队列
@property (nonatomic, strong) NSOperationQueue *queue;

// 存放所有的下载操作 (url是key,operation对象是value)
@property (nonatomic, strong) NSMutableDictionary *operations;

// 存放所有下载完的图片
@property (nonatomic, strong) NSMutableDictionary *images;

@end

@implementation LMTableViewController

#pragma mark 懒加载
- (NSMutableArray *)apps {

    if (!_apps) {
        // 1.加载plist
        NSString *file = [[NSBundle mainBundle] pathForResource:@"apps" ofType:@"plist"];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];

        // 2.字典转化为模型存储在数组中
        NSMutableArray *appArray = [NSMutableArray array];

        for (NSDictionary *dict in dictArray) {
            LMApp *app = [LMApp appWithDict:dict];
            [appArray addObject:app];
        }
        // 3.赋值
        self.apps = appArray;
    }
    return _apps;
}
// 其他的getter就不显示了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *ID = @"app";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }

    // 取出模型
    LMApp *app = self.apps[indexPath.row];

    // 设置基本信息
    cell.textLabel.text = app.name;
    cell.detailTextLabel.text = app.download;

    // 先从images缓存中取出图片url对应的UIImage
    UIImage *image = self.images[app.icon];
    if (image) { // 说明图片已经下载成功过 (成功缓存)
        cell.imageView.image = image;
    } else { // 说明该图片并没有下载成功过 (并未缓存过)
        // 显示占位图片
        cell.imageView.image = [UIImage imageNamed:@"placeholder"];

        // 下载图片
#warning 下载图片
        [self download:app.icon indexpath:indexPath];
    }
    return cell;
}
- (void)download:(NSString *)imageUrl indexpath:(NSIndexPath *)indexPath {    
    // 取出当前图片的url对应的下载操作 (operation对象)
    NSBlockOperation *operation = self.operations[imageUrl];
    if (operation) return;   
    // 创建操作,下载图片
    __weak typeof(self) appVc = self;
    operation = [NSBlockOperation blockOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:imageUrl];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];       
        // 回到主线程
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            // 存放图片到字典中
            if (image) {
                appVc.images[imageUrl] = image;
            }
            // 从字典中移除下载操作 (防止operations越来越大,保证下载失败后能重新下载)
            [appVc.operations removeObjectForKey:imageUrl];           
            // 刷新表格
            [appVc.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
    }]; 
    // 添加操作到队列中
    [self.queue addOperation:operation];
    // 添加到字典中(这句话是为了解决重复下载)
    self.operations[imageUrl] = operation;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.   
    // 移除所有的下载操作缓存
    [self.queue cancelAllOperations];
    [self.operations removeAllObjects];
    // 移除所有的图片缓存
    [self.images removeAllObjects];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    // 暂停下载
    [self.queue setSuspended:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 回复下载
    [self.queue setSuspended:NO];
}

猜你喜欢

转载自blog.csdn.net/M_15915899719/article/details/82707334