【问题标题】:Loading images from a URL into a UITableViewCell's UIImageView将图像从 URL 加载到 UITableViewCell 的 UIImageView
【发布时间】:2011-12-05 18:37:47
【问题描述】:

我有以下代码:(注意:newsImageURLNSArray

NSString *imagesURL = @"http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage02.png,http://aud.edu/images/newsimage03.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,";
newsImageURL = [[NSArray alloc] initWithArray:[AllNewsHeadLine componentsSeparatedByString:@","]];

我正在尝试使用以下代码将这些图像加载到单元格中:

NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]]; 
cell.image = [UIImage imageWithData:imageData];

当我改用这条线时,图像加载正常:

cell.image = [UIImage imageNamed:@"imagename.png"];

我做错了什么?

【问题讨论】:

  • 您拥有的代码会发生什么?是否加载了一些图像,或者您是否每次都得到一个空白区域?
  • 当我尝试从 URL 加载它时,图像的空间消失了,没有显示任何图像,也没有任何反应。但是当我使用 imageNamed 加载它时,它加载得很好
  • 在您调用将图像数据加载到 NSData 对象 imageData 之后,您是否检查过它是否实际上包含任何数据 [imageData 长度];
  • 好吧,它返回NULL,所以我猜这就是问题所在,但我真的不知道为什么!!!网址在那里!!
  • 查看实例化数组的行,你说 [AllNewsHeadLine componentsSe... 而从示例中,它应该是 NSArray *newsImageURL = [imagesURL componentsSeparatedByString:@","]跨度>

标签: objective-c ios ios4


【解决方案1】:

您应该使用支持缓存、默认占位符和延迟加载图像的现有框架。

https://github.com/rs/SDWebImage 是一个好用又简单的框架

#import "UIImageView+WebCache.h"

...

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

    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

    // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

【讨论】:

  • 他问为什么上面的代码不起作用......我也想知道......添加框架不是正确的答案,也没有帮助
【解决方案2】:

你可以这样加载数据:

NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]];

您也可以通过这种方式实例化 URL 数组:

NSArray *newsImageURL = [imagesURL componentsSeparatedByString:@","];

但是,如果有人在桌子上滚动很多次,您最终可能会在单元格被回收时多次加载图像。

【讨论】:

    【解决方案3】:

    也许你可以试试https://github.com/SpringOx/ALImageView.git。它比SDWebImage简单得多。你只需要两个源文件(ALImageView.h/ALImageView.m)。你可以重用图像视图在一个tableview单元格中重新加载不同的url .

    1. 支持本地和内存缓存;
    2. 支持占位符;
    3. 支持点击触摸(target-action);
    4. 图像视图的支持角;

    【讨论】:

      【解决方案4】:

      您可以在以下代码的帮助下轻松加载所有图像,详细信息数组是一个主数组

      Details Array :-  {
              "item_code" = 709;
              "item_desc" = Qweqweqwe;
              "item_name" = AQA;
              "item_photo" = "http://toshaya.com/webapp/snap&sell/api/img_items/709.png";
              "item_price" = "0.00";
              "item_till" = "20-25";
              "item_type" = Orange;
              latitude = "";
              longitude = "";
          }
      

      借助以下代码将照片 URL 检索为字符串

       NSString * result = [[DetailArray objectAtIndex:indexPath.row]objectForKey:@"item_photo"]; //componentsJoinedByString:@""];
      
       NSLog(@"%@",result);
      
      NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:result]];
      cell.icon.image = [UIImage imageWithData:imageData];
      

      【讨论】:

        猜你喜欢
        • 2013-09-01
        • 1970-01-01
        • 2012-10-29
        • 1970-01-01
        • 2013-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-19
        相关资源
        最近更新 更多