hotdingyang23 2014-06-27 14:16 采纳率: 0%
浏览 6420

ios开发 UITableView异步加载图片,滑动时会很卡

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cellIdentifier";

BrandTableViewCell *cell = (BrandTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];//通过xib自定义的一个cell,包括图片+title;
if (cell==nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"BrandTableViewCell" owner:self options:nil] objectAtIndex:0];
}
   if (logoUrlStr!=nil) {
        NSString *md5Str = [logoUrlStr MD5Hash];
        NSString *path = NSHomeDirectory();
        path = [path stringByAppendingPathComponent:@"tmp"];
        path = [path stringByAppendingPathComponent:md5Str];
        NSFileManager *fManager = [[NSFileManager alloc]init];
        if ([fManager fileExistsAtPath:path]) {
            NSData *data = [NSData dataWithContentsOfFile:path];
            cell.brandImgView.image = [UIImage imageWithData:data];

//以上是图片缓存;
}else{
if (brandTableView.dragging == NO && tableView.decelerating == NO) {
MyConnection *myCon = [[MyConnection alloc]initWithUrl:logoUrlStr Delegate:self];
myCon.tag = indexPath.row;
}
//异步下载图片,代理方法中写入缓存,并更新cell.brandImgView.image = [UIImage imageWithData:data];

}
}
return cell;
}

代理方法如下:
-(void)MyConnectionDidFinish:(MyConnection *)con Data:(NSData *)data{
BrandTableViewCell *cell = (BrandTableViewCell *)[brandTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:con.tag inSection:0]];
cell.brandImgView.image = [UIImage imageWithData:data];
}

滑动TableView时明显感觉界面很卡。多滑动几次,会引起手机重启。

  • 写回答

1条回答

  • xiezi222 2014-08-17 03:01
    关注

    个人认为是因为你所有的东西都在cellForRow 里面操作,尤其是本地读取这块,所以会造成卡的现象,而且滑动后会创建多个本地读取,可能对性能上消耗很大,建议你开线程,把你的数据源在线程里组织好了,然后主线程拿出来在cellForRow 里面直接用,这样应该会好,你可以试试。

    评论

报告相同问题?

悬赏问题

  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码