objective-c - 将图像从 URL 加载到 UITableViewCell 的 UIImageView

标签 objective-c ios ios4

我有以下代码:(注意:newsImageURL 是一个 NSArray)

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"];

我做错了什么?

最佳答案

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

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;
}

关于objective-c - 将图像从 URL 加载到 UITableViewCell 的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8390151/

相关文章:

ios - 如何根据 NSIndexPaths 数组从 NSArray 中选择元素

iphone - 带缩放 mapView 的点触摸

ios - 添加一个 UIImageView 到 NavigationBar 的后面(不是背景图片)

ios - Dynamo DB iOS dynamoDBObjectMapper存储双倍

ios - 禁用实时应用程序的 TestFlight 检查点

objective-c - 将滤镜效果应用到 UIImage

objective-c - 如何在特定地址初始化对象(NSObject子类)

ios - 多个按钮的设计模式

iphone - uitableview 在滚动到顶部时自动更新

ios - 将变量从 tableView 传递到 prepareForSegue?