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 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题