ios - 从web url xcode下载并查看图像

时间:2014-09-29 12:46:53

标签: ios uiimageview

我需要从网址下载图片并将其加载到UIIamge视图

首先我从URL获取图像,但数据返回nil我不知道为什么?

 -(UIImage *) getImageFromURL:(NSString *)fileURL {
        UIImage * result;
    // data here return with nil 
        NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
        result = [UIImage imageWithData:data];

        return result;
    }

2 个答案:

答案 0 :(得分:2)

您可以使用AFNetworking

只需使用:- (void)setImageWithURL:(NSURL *)url;

或更复杂的事情:

UIImageView image;
NSString *urlstring = [NSString stringWithFormat:@"http://url/"];
NSURL *url = [NSURL URLWithString:urlstring];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[image setImageWithURLRequest:request
              placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                       success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                            // Succes on loading image
                       }
                       failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                            // Fail to load image
                       }];

答案 1 :(得分:2)

您可以通过以下网址链接设置图片:

  NSString *your_url = @"http://your_link/";
  image_View.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL   URLWithString:your_url]]]; 

另请参阅:

1)question 1

2)question 2

3)question 3