如题,UITableView里的每一个cell都有图片,有的cell最多有9张,图片我都是已经下载到沙盒里的,那么我滑动tableView的时候就会出现卡顿,这种情况下该怎么处理这个图片呢?

解决方案 »

  1.   

    这要看你读取图片的逻辑是如果处理的,如果当要显示图片时再从沙盒中读取这样多少会有一点卡,可以考虑cache一定数量的UIImage到内存里。或直接用第三方代码,比如:https://github.com/kirillsh/ImageLoader-iOS
      

  2.   

    额,我的是下载图片到本地,然后在存本地的时候直接加载到内存里了,显示的时候直接用的内存里的image,也就是说我在下载的时候直接就存在内存里了,然后显示的时候直接显示的内存里的图片,本地我只是为了下载请求的时候不至于资源浪费
      

  3.   

    是否图片过大?可以缩小后生成UIImage再进行显示
      

  4.   

    检查一下你的代码在主线程中有没有用到这个函数- (BOOL)fileExistsAtPath:(NSString *)path,这个函数最好放到NSOperation中去,异步使用。在主线程中这个函数会阻塞UI,就是你滑动cell太快的话,频繁的去判断图片是否已经下载过到本地,会造成Cell一卡一卡的。建议你用SDWebImage去缓存图片,然后加载图片的时候,选择异步回调。图片加载成功的时候用tableview的beginUpdate和endUpdate。- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        PJJokeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextJokeCell"];
        if (cell == nil) {
            cell = [[PJJokeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextJokeCell"];
        }
        PJJoke *joke = self.jokes[indexPath.row];
        cell.time.text = joke.time;
        cell.text.text = joke.text;
        if (joke.imgurl && joke.imgurl.length > 0) {
            SDImageCache *imageCache = [SDImageCache sharedImageCache];
            if ([imageCache diskImageExistsWithKey:joke.imgurl]) {
                [cell.image setImage:[imageCache imageFromDiskCacheForKey:joke.imgurl]];
            } else {
                [cell.image setImageWithURL:[NSURL URLWithString:joke.imgurl]
                                  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                                      if (error == nil && image && joke && tableView) {
                                          joke.imageWidth = image.size.width;
                                          joke.imageHeight = image.size.height;
                                          [tableView beginUpdates];
                                          [tableView endUpdates];
                                      }
                                  }];
            }
        }
        return cell;
    }- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        PJJoke *joke = self.jokes[indexPath.row];
        SDImageCache *imageCache = [SDImageCache sharedImageCache];
        if ([imageCache diskImageExistsWithKey:joke.imgurl]) {
            UIImage *image = [imageCache imageFromDiskCacheForKey:joke.imgurl];
            joke.imageWidth = image.size.width;
            joke.imageHeight = image.size.height;
        }
        return [PJJokeTableViewCell heightForJoke:self.jokes[indexPath.row]];
    }
      

  5.   

    tableView的beginUpdate和endUpdate要比reloadData和reloadRowsAtIndexPaths好,因为beginUpdate和endUpdate会执行一个动画block,图片加载的时候显的很平滑。你自己试一下就知道了。
      

  6.   

    Subclassing UITableViewCell
    1、添加subView至contentView,若总共只有3到4个subViews,这不失为一个好的方案。但若超过4个subViews,tableView的滑动性能将会打折扣。2、自绘cell的content。通过添加一个自定义的View到contentView,然后实现自定义View中的drawRect:方法,将图片绘制上去。
    但当cell进入可编辑模式,如果reorder控件性能将会打折扣。同时在做动画的时候不要执行自定义的绘制。
    如果cell中的控件需要响应UI,那么这种方案不合适.
      

  7.   

    图片加到一个数组中,在cell中到数组中查(按下标),这也是一种方法,你这样感觉好麻烦啊 
      

  8.   

    cell复用机制是否开启?