我需要从网址下载图片并将其加载到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;
}
答案 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]]];
另请参阅: