2016-03-14 70 views
1

_pageImages是nsmutableArry我存储图像的URL。NSData到uiimage返回null

_pageImages = [[NSMutableArray alloc]initWithObjects:@"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com",@"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com", nil]; 

想要在uiimageview中显示此URL图像。

NSURL *url = [NSURL URLWithString:[_pageImages objectAtIndex:i]]; 

NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 

UIImage *tmpImage = [UIImage imageWithData:data]; 

_backgroundImageView.frame = CGRectMake(0 ,10, 320, 320); 

_backgroundImageView.image = tmpImage; 

    [self.view addSubview:_backgroundImageView]; 

我看到了很多例子,但我不能得到正确的解决方案。新的发展。帮助我..

+2

您是否检查过您的数据不是零?因为也许你只是忘记禁用目标域名的应用传输安全 (当我尝试https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com我自己,我在浏览器中出现错误...) – CZ54

+0

是的如何使用返回NSError对象的方法(例如'initWithContentsOfURL:options:error:')。 – trojanfoe

+0

你可以在这里找到http://stackoverflow.com/questions/30096806/load-uiimage-from-url-in-ios –

回答

0

的问题是在URL试试这个!

试着访问您的图片网址,我得到的全部是“未授权”。

运行此示例代码,您的代码,只是使用不同的图像地址。 检查正确的图像地址,它应该按预期运行。

NSMutableArray *pageImages = [[NSMutableArray alloc]initWithObjects:@"http://findicons.com/files/icons/1636/file_icons_vs_3/128/url.png",@"http://findicons.com/files/icons/1636/file_icons_vs_3/128/pdf.png", nil]; 

NSURL *url = [NSURL URLWithString:[pageImages objectAtIndex:0]]; 

NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 

UIImage *tmpImage = [UIImage imageWithData:data]; 

_depImage.frame = CGRectMake(0 ,10, 320, 320); 
_depImage.image = tmpImage; 
+0

我改变了网址,但仍然存在UIImage * tmpImage = [UIImage imageWithData:data]; ... tmpImage返回null。 – Saravananr

+0

_backgroundImageView.image = tmpImage;是空 – Saravananr

+0

thanx你这么多... – Saravananr

0

通过图像发送AsynchronousRequest downloading--

NSURL *url = [NSURL URLWithString:[_pageImages objectAtIndex:i]]; 

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    _backgroundImageView.image = [UIImage imageWithData:data]; 
}]; 
+0

如何使用异步通信来解决问题? – trojanfoe

+0

_backgroundImageView.image = tmpImage;为空 – Saravananr

+0

@Saravananr高兴地帮助你。 :) –

0

我们可以简单地使用下面从URL

加载图像数据
_backgroundImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLwithString:urlString]]]; 

没有必要在这里调用一个服务调用的。

希望它有帮助..