iOS中如何优化Cell中图片的下载性能

在iOS开发中使用最为常见的是UITableView,其中UITabelViewCell中下载图片,会影响用户下拉刷新UI,导致卡顿,用户体验不好,在这篇blog中,我将以一个例子来说明如何优化UITableView下载图片
1.使用懒加载方式,首先将CellData数据加载进来
// lazy
- (NSMutableArray*)apps {
if (!_apps) {
NSString *path = [[NSBundle mainBundle]pathForResource:@”apps.plist” ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];

    NSMutableArray *appArray = [NSMutableArray array];
    for (NSDictionary *dict in dictArray) {
        App *app = [App appWithDict:dict];
        [appArray addObject:app];
    }
    _apps = appArray;
}
return _apps;

}

2.然后在UITaleView的dataSource方法中加载数据
//Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.apps.count;
}
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @”app”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];

if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
App *app = self.apps[indexPath.row];
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;

UIImage *image = self.images[app.icon];
if (image) {
    cell.imageView.image = image;
} else {
    cell.imageView.image = [UIImage imageNamed:@"placeholder"];
    // 下载图片
    [self download:app.icon indexPath:indexPath];
}

return cell;

}

(1)其中在加载图片过程中首先判断图片是否已经下载过,如果下载过直接加载,如果没有下载过那么就需要下载,并且需要填写一个默认的图片
其中判断图片是否已经下载过是使用字典查询的
- (NSMutableDictionary *)images {
if (!_images) {
_images = [[NSMutableDictionary alloc]init];
}
return _images;
}
下载图片,是需要创建一个线程来执行下载,首先应该判断是否下载线程已经执行,如果执行,不需要重复下载,因此这个线程也是与一个imageURL来绑定

  • (void)download:(NSString )imageUrl indexPath:(NSIndexPath )indexPath {
    NSBlockOperation *operation = self.operations[imageUrl];
    // 如果存在操作则直接返回,防止已经在下载的操作重复下载
    if (operation) {
    return;
    }
    __weak typeof (self) appsVc = self;
    operation = [NSBlockOperation blockOperationWithBlock:^{
    NSURL *url = [NSURL URLWithString:imageUrl];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        if (image) {
            appsVc.images[imageUrl] = image;
        }
        [appsVc.operations removeObjectForKey:imageUrl];
        [appsVc.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
    

    }];
    [self.queue addOperation:operation];

    self.operations[imageUrl] = operation;

}
为进一步优化,应在用户滑动过程中停止下载,结束拖拽时候开始下载
/**
* 当用户开始拖拽表格时调用
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 暂停下载
[self.queue setSuspended:YES];
}

/**
* 当用户停止拖拽表格时调用
*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// 恢复下载
[self.queue setSuspended:NO];
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值