1

UIImage not showing up from NSURL - driving me nuts. It's SO basic yet won't show up. I know the URL is good because if I go to it directly on the web it pulls up the image. I'm sure I'm missing something stupid but any help is appreciated, thanks!

UIImageView *test = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
NSURL *url = [[NSURL alloc] initWithString:@"http://is5.mzstatic.com/image/thumb/Music/v4/0b/9c/d2/0b9cd2e7-6e76-8912-0357-14780cc2616a/source/100x100bb.jpg"];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
[test setImage:[UIImage imageWithData:data]];
[self.view addSubview:test];
[test setCenter:self.view.center];
Clayton C.
  • 863
  • 1
  • 8
  • 17
  • Is `data` nil? Is `[UIImage imageWithData:data]` nil? – Larme Jul 02 '16 at 10:12
  • the code is totally correct. Just copy pasted the code into my project and the image view shows the image without any problems. – Andrey Chernukha Jul 02 '16 at 10:14
  • @AndreyChernukha Thanks - I'm a Metallica nuthead. Yeah that's weird, the code looked right to me but I have absolutely no idea why it won't work. I even copied and pasted it into a brand new project and still the same result. – Clayton C. Jul 02 '16 at 11:56
  • If you send me your project I think good chances are I'll make it work. I'm a Metallica nuthead too so you can trust me))) – Andrey Chernukha Jul 02 '16 at 11:58
  • @Larme No it's there, Andrey got it working no problem. – Clayton C. Jul 02 '16 at 11:58
  • @AndreyChernukha Thanks...I'm gonna get some sleep and take another crack at it when I'm up. If I still haven't gotten it you can take a look. Much obliged. – Clayton C. Jul 02 '16 at 11:59

3 Answers3

1

Code correct but you need enable Transport security. See this link Transport security has blocked a cleartext HTTP

Community
  • 1
  • 1
erim kurt
  • 91
  • 6
0

Check line by line and change code

UIImageView *test = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
NSURL *url = [NSURL URLWithString:@"http://is5.mzstatic.com/image/thumb/Music/v4/0b/9c/d2/0b9cd2e7-6e76-8912-0357-14780cc2616a/source/100x100bb.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[test setImage:[UIImage imageWithData:data]];
[self.view addSubview:test];
[test setCenter:self.view.center];
Amit Jagesha シ
  • 1,092
  • 12
  • 21
  • The data is not null, the image is not null, nothing is null. The only problem is that it simply does not show up. I've done nullity checks at every level, NSLogs at every level, breakpoints...everything gets hit, everything works - until I run it. – Clayton C. Jul 03 '16 at 06:44
  • you get image in imageview ? – Amit Jagesha シ Jul 03 '16 at 07:05
0

Never use dataWithContentsOfURL to fetch a non-local URL. It is designed for loading data from files, not network URLs. Although it will load data from network URLs, it does so synchronously on the current thread, and if you're working with UIView objects, that means this is on the main thread.

If you do it the way you're trying to do this above, your app will get rejected from the App Store with almost 100% certainty, because on a slow network, the image loading will block long enough for iOS to kill your app outright for blocking the main thread for too long.

To make a network request, you must use NSURLSession (or NSURLConnection if you're trying to maintain backwards compatibility with iOS 6 and earlier) to retrieve the image, and then when the request finishes loading, create a UIImage with that data and then call setImage on an already-displayed UIImageView (which should be showing some sort of "loading" icon up until that point).

It might be easier to use one of the many third-party networking libraries that provide classes specifically for the purpose of making network image loading less painful.

With that said, I don't see why your existing code doesn't show anything (assuming the server responds quickly enough) unless you have a problem with the bounds or with autolayout constraints causing the image view itself to not be visible or to be clipped.

dgatwood
  • 10,129
  • 1
  • 28
  • 49