【问题标题】:Lazy loading of image in tableview在tableview中延迟加载图像
【发布时间】:2014-02-13 22:29:00
【问题描述】:

我正在尝试以惰性模式加载我的 uitableviewcells 的图像。

我正在尝试以最简单的方式来做这件事,我看到了很多例子,但它们比我的目的更进一步。

这是我目前正在做的事情,但它不起作用:

// Configure the cell...
    Info *info = [self.Array objectAtIndex:indexPath.row];
    cell.textLabel.text = info.name;
    cell.detailTextLabel.text = info.platform;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    //The image is downloaded in asynchronous 

    NSBlockOperation *downloadingImgBlock = [NSBlockOperation blockOperationWithBlock:^{
        NSString* imageURL = info.imgURL;
        NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]];
        cell.imageView.image = [UIImage imageWithData:imageData];
    }];
[self.queue addOperation:downloadingImgBlock];

为什么它不起作用?它是如何工作的?

【问题讨论】:

  • 你是否尝试在主线程上设置图像,调用dispatch_async并通过主线程
  • 你试过Grand Dispatch了吗?
  • 两个问题都没有,你能告诉我怎么做吗?

标签: iphone objective-c ios


【解决方案1】:

伙计,AsyncImageView 是你的朋友!只需设置图像 url,一切都会为您处理,下载,缓存。太棒了。

【讨论】:

  • 我不能使用库,它必须在我的代码中以最简单的方式完成,但是谢谢
  • 我不能使用第三部分组件
【解决方案2】:

试试下面的代码

  ......
  __block UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  __block UIImage *img;
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imageURL]]];

        dispatch_async(dispatch_get_main_queue(),^{

            cell.imageView.image = img;

            }
        });


    });
  ......

【讨论】:

    【解决方案3】:

    使用 3rd 方库源代码是最简单的,但您可以在代码中执行此操作。您将要创建一个NSMutableArray 作为.h 中的属性或.m 顶部的属性,如下所示:

    @implementation viewController{
         NSMutableArray *picList;
    }
    

    -initWithCoder: 或者,无论你重载什么初始化:

    picList = [[NSMutableArray alloc] init]; 
    

    – tableView:cellForRowAtIndexPath:(fullPicURL 是 URL):

    if([self imageExists:fullPicURL]){
    
        shoePic = [picList objectForKey:fullSmallPicURL];   
    
    } else {
    
        NSURL *picURL = [NSURL URLWithString:fullPicURL];
    
        shoePic = [UIImage imageWithData:[NSData dataWithContentsOfURL:picURL]];
        NSDictionary *thisImage = [NSDictionary dictionaryWithObject:shoePic forKey:fullSmallPicURL];
        [cachedImages addEntriesFromDictionary:thisImage];
    } 
    

    -imageExists: 是您编写的一个函数,用于检查字典中的 url。对象是图片本身,关键是url。然后你做cell.image = showPic;,或者你想调用的任何东西,你就完成了缓存。要异步加载它们,请执行以下操作:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
        NSData *data = [NSData dataWithContentsOfURL:shoes];
        [self performSelectorOnMainThread:@selector(fetchedShoeData:) withObject:data waitUntilDone:YES];
    
    });
    

    在 fetchedData 结束时:

    [self.tableView reloadData];
    

    然后确保您编辑–numberOfSectionsInTableView: 以在必要时更改自身。您可能需要做一些其他事情才能使其 100% 正常工作,请告诉我

    【讨论】:

      【解决方案4】:

      试试这个,我一直使用这个方案异步加载图像:

      (我还没有找到最简单的方法。)

      - (void)inAnyMethod {
      
          // ...
      
          void (^blockLoadPhoto)() =  ^{
              UIImage *_imageTemporary = [UIImage imageNamed:@"..."];
              if (_imageTemporary) {
                  [self performSelectorOnMainThread:@selector(setPhoto:) withObject:_imageTemporary waitUntilDone:true];
              }
          };
      
          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), blockLoadPhoto);
      
          // ...
      
      }
      
      - (void)setPhoto:(UIImage *)photo {
          // do something with the loaded image
      }
      

      【讨论】:

        【解决方案5】:

        我终于设法通过以下方式设置图像:

        – performSelectorOnMainThread:withObject:waitUntilDone:
        

        图片下载后

        【讨论】:

          猜你喜欢
          • 2023-03-22
          • 2010-10-06
          • 2010-11-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-24
          • 2021-08-15
          相关资源
          最近更新 更多