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 里面直接用,这样应该会好,你可以试试。

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大