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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘