简体   繁体   English

NSData到uiimage返回null

[英]NSData to uiimage return null

_pageImages is nsmutableArry I'm store the image url. _pageImages是nsmutableArry我存储了图片网址。

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

Want to display this url images in uiimageview. 想在uiimageview中显示此网址图片。

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

I saw many examples but i can't get the proper solution. 我看到很多例子,但我无法得到适当的解决方案。 new for development. 新的发展。 help me.. 帮我..

try this by sending AsynchronousRequest for image downloading-- 通过发送AsynchronousRequest进行图像下载来尝试此操作-

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

The problem is in the URL! 问题在于URL!

Try to access your images URL, all I got is " Unauthorized ". 尝试访问您的图片网址,我得到的只是“ 未经授权 ”。

Run this sample code, your code, just using a different image address. 仅使用其他图像地址运行此示例代码,即您的代码。 Check for the correct image address, and it should run as expected. 检查正确的图像地址,它应该按预期运行。

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;

We can simply use following to Load Image data from URL 我们可以简单地使用以下命令从URL加载图像数据

_backgroundImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLwithString:urlString]]];

No need of Calling a service call here. 无需在此处拨打服务电话。

Hope it helps.. 希望能帮助到你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM