0

Evening, I'm trying to set an ImageView.image (an IBOutlet in this case) with an image downloaded from the web.

I've already ridden a lot of questions about this in stack overflow.

And I've found this common answer:

 NSURL *url = [NSURL URLWithString:@"http://www.fnordware.com/superpng/pnggrad16rgb.png"];

 NSData *data = [NSData dataWithContentsOfURL:url];

 NSLog(@"imageData: %@", data);

 UIImage *image = [UIImage imageWithData:data];

 NSLog(@"image: %@", image);

 self.imageView.image = image;

But of course in my case does not work. My logs are always null.

Can you please tell me which is the problem?

7
  • what the result you get here url Commented Nov 16, 2016 at 11:01
  • "fnordware.com/superpng/pnggrad16rgb.png". The right one. Commented Nov 16, 2016 at 11:03
  • 1
    Did you any log regarding Transport security exception Commented Nov 16, 2016 at 11:04
  • your problem solved or still there Commented Nov 16, 2016 at 11:10
  • 1
    The problem Was about Transport Security Commented Nov 16, 2016 at 11:10

3 Answers 3

3

This URL is HTTP type so you need to set NSAppTransportSecurity at your plist file.

For that process is

 <key>NSAppTransportSecurity</key>
 <dict>
      <key>NSAllowsArbitraryLoads</key>
     <true/>
 </dict> 

enter image description here

This might help you. Please let me know if having any problem regarding this.

Sign up to request clarification or add additional context in comments.

1 Comment

why you posted the same answer again
2

I believe you are trying to run it on iOS 8 +

Please add this to your info.plist

 <key>NSAppTransportSecurity</key>
   <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
   </dict>

And everything should be fine :)

WHY ??

iOS 8 onwards http is not allowed you have to use either https or add exception to your info.plist :)

2 Comments

That was the problem! :)
@the-miotz : Thanks buddy :)
1

you should use SDWebImage library. It can cache image from url also. and use very simple

[yourImageView sd_setImageWithURL:[NSURL URLWithString:@"image-url"] placeholderImage:[UIImage imageNamed:@"photo.jpg"]]

place holder image is showed in case the image hasn't load yet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.