Objective c 如何将图像从URL加载到UITableViewCell';什么是UIImageView?

Objective c 如何将图像从URL加载到UITableViewCell';什么是UIImageView?,objective-c,uiimageview,Objective C,Uiimageview,试图从JSON获取图像并在tableview单元格上显示,但在转换为数据时,图像变为nill dispatch_async(dispatch_get_global_queue(0,0), ^{ NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]]; if ( data == nil ) return;

试图从JSON获取图像并在tableview单元格上显示,但在转换为数据时,图像变为nill

dispatch_async(dispatch_get_global_queue(0,0), ^{
    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]];
    if ( data == nil )
        return;
    dispatch_async(dispatch_get_main_queue(), ^{
        // WARNING: is the cell still using the same data by this point??
        cell.imageView.image = [UIImage imageWithData: data];
    });
});
对于NSData*数据,它总是显示为零,即使我硬编码喜欢

  NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/American_Beaver.jpg/220px-American_Beaver.jpg"]];
它在数据变量中显示为零

我做错什么了吗?还有其他最好的方法吗

您可以使用对图像进行延迟加载

#import <SDWebImage/UIImageView+WebCache.h>

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:IMAGE_URL]
             placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
#导入
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:IMAGE_URL]
占位符图像:[UIImage ImageName:@“placeholder.png”];

此代码正常工作,您可能从json获取了错误的图像URL

 dispatch_async(dispatch_get_global_queue(0,0), ^{
            NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/American_Beaver.jpg/220px-American_Beaver.jpg"]];
            if ( data == nil )
                return;
            dispatch_async(dispatch_get_main_queue(), ^{
                // WARNING: is the cell still using the same data by this point??
                UIImage *image = [UIImage imageWithData:data];
                NSLog(@"%@",image);

            });
        });

您尝试过这个url吗?url是正确的,只是我错把两个不同的url放在了问题中
#import "SDWebImage/UIImageView+WebCache.h"

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.freeiconspng.com/uploads/flower-png-22.png"]
         placeholderImage:[UIImage imageNamed:@"placeholder.png"]];