【问题标题】:Adding images to UITableViewCell from an array of URL of images从图像的 URL 数组将图像添加到 UITableViewCell
【发布时间】:2016-02-17 11:20:52
【问题描述】:

我有一组图像 URL,我想在 UITableView 的单独单元格上显示每个 URL 的图像。在我之前使用的代码中:

    UIImageView *view_Image = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, 400, 180)];

    view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arrURL[i]]]];

这是在一个 for 循环中,因此 x、y 和 i 的值会随每个 UIImageView 发生变化。但是,当我在cellForRowAtIndexPath 中使用它时,这种方法不起作用。图片未显示,我对cellForRowAtIndexPath 的实现如下:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *simpleIdentifier = @"simpleIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
}

cell.textLabel.text = _arrNames[indexPath.row];

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 300, 150)];
imv.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arr[i]]]];

[cell.contentView addSubview:imv];
return cell;
}

另外,当我直接添加图像的 URL 时,图像会显示在每一行中,但滚动会滞后很多。任何帮助将不胜感激。谢谢。

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

使用 sdwebimage 库sdwebImage

【讨论】:

    【解决方案2】:

    使用下面的代码在后台下载图像并将其设置为 CELL 的 IMAGEVIEW。

    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 300, 150)];
    [imv sd_setImageWithURL:[NSURL URLWithString:arrURL[i]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    [cell.contentView addSubview:imv];
    

    希望对你有所帮助。

    【讨论】:

      【解决方案3】:

      使用这个库,它会帮助你。

      https://github.com/nicklockwood/AsyncImageView

      它平滑地滚动uitableview。

      【讨论】:

        【解决方案4】:

        在 cellForRowAtIndexPath 中,检查缓存中图像的可用性(第一次它不会在那里),如果存在则分配它,如果不存在则开始下载。

        下载完成后,将图像保存在缓存中并重新加载 tabeview,然后调用 cellForRowAtIndexPath,但这次图像将在缓存中可用。

        我还没有涵盖所有边缘情况,例如处理下载失败,希望您能理解。

        【讨论】:

          【解决方案5】:
          UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 300, 150)];
          [imv sd_setImageWithURL:[NSURL URLWithString:arrURL[i]] placeholderImage:    [UIImage imageNamed:@"placeholder.jpg"]];
          [cell.contentView addSubview:imv];
          

          【讨论】:

            【解决方案6】:

            您可以使用 AFNetworking 库。

            在您的视图控制器中导入此标头

            #import "UIImageView+AFNetworking.h"
            

            使用以下代码下载图片:

            [imv setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.imageURL]] placeholderImage:[UIImage imageNamed:@"placeholder_gallery.png"];
            

            【讨论】:

              【解决方案7】:

              您可以下载库SDWebImage,然后将其导入您的项目中。

              #import "UIImageView+UIActivityIndicatorForSDWebImage.h"
              

              然后在您的UITableView 委托方法中编写以下代码。

              -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
              NSString *simpleIdentifier = @"simpleIdentifier";
              
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
              if(cell == nil)
              {
                  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
              }
              
              cell.textLabel.text = _arrNames[indexPath.row];
                  NSURL *imgURL = [NSURL URLWithString:@"Your URL String"];
              
                  [cell.imgView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"no-image"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                      if (image) {
                          cell.imv.image = image;
                      }else{
                          cell.imv.image = [UIImage imageNamed:@"no-image"];
                      }
                  }];
              
              [cell.contentView addSubview:imv];
              return cell;
              }
              

              也许这对你有帮助。

              【讨论】:

                猜你喜欢
                • 2017-07-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-07-31
                • 2012-10-29
                相关资源
                最近更新 更多