zoukankan      html  css  js  c++  java
  • IOS开发中如何解决TableView中图片延时加载

    经常我们会用tableView显示很多条目, 有时候需要显示图片, 但是一次从服务器上取来所有图片对用户来浪费流量, 对服务器也是负担.最好是按需加载,即当该用户要浏览该条目时再去加载它的图片。

      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
      {  
          UIImage *image = [self getImageForCellAtIndexPath:indexPath];  //从网上取得图片  
          [cell.imageView setImage:image];  
      } 

      这虽然解决了延时加载的问题, 但当网速很慢, 或者图片很大时(假设,虽然一般cell中的图很小),你会发现程序可能会失去对用户的响应.

      原因是

      UIImage *image = [self getImageForCellAtIndexPath:indexPath];  

      这个方法可能要花费大量的时间,主线程要处理这个method.

      所以失去了对用户的响应.

    所以需要做如下改写:

      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
      {  
          [self  performSelectorInBackground:@selector(updateImageForCellAtIndexPath:) ];  
      } 
    
      - (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath  
      {  
              @autoReleasePool{ 
          UIImage *image = [self getImageForCellAtIndexPath:indexPath];  
          UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];  
          [cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];  
        } 
      } 
  • 相关阅读:
    正则表达式30分钟入门教程
    Literal控件用法
    用asp.net 2.0实现网站二级域名
    IIS7 asp.net URL重写配置
    用VS2008开发Ajax网站需要注意
    C#中的equals与==的比较,赋加compareto
    nvchar(50)与Substring(0,50)的理解
    利用Mircosoft URLRewriter.dll实现asp.net页面伪静态
    .NET命名规范中文版
    iframe动态改变src的测试
  • 原文地址:https://www.cnblogs.com/liuziyu/p/4348785.html
Copyright © 2011-2022 走看看