简体   繁体   中英

NSData to uiimage return null

_pageImages is nsmutableArry I'm store the image url.

_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.

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

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!

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

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

No need of Calling a service call here.

Hope it helps..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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