Load UIImage from URL in iOS
Asked Answered
H

4

5

I am trying to load a UIImage from its URL with completionand placeholder image , but neither the UIImage nor the placeholder UIImage are loaded.

Here is my code:

  NSURL *url = [NSURL URLWithString:@"http://assets.gearlive.com/tvenvy/blogimages/stewiegriffin.jpg"];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];

  [self.imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"bg.png"]

      success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
          imageView.image = image;
      } 
      failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {}
   ];
Holle answered 7/5, 2015 at 9:19 Comment(4)
Your code seems correct. Just set the placeholder directly self.imageView.image = [UIImage imageNamed:@"bg.png"]; to make sure your image view is visible and setting the image works..Duester
If setting the image directly does not work, set image view's background color self.imageView.backgroundColor = [UIColor redColor]; and run the app to make sure the image view is acually visible on the screen..Duester
@AdhRadwan i have posted answer try it and inform me.Cathryncathy
Thanks @lukya your comment is realy helpfulHolle
E
13

Use AFnetworking where you can set placeholder image also if the url is not correct. This is more efficient when you are loading images in a tableview or a collection view.

Import UIImageView+AFNetworking.h

#import "UIImageView+AFNetworking.h"

and us the following code:

NSString *url1=@"http://www.fnordware.com/superpng/pnggrad16rgb.png";
[self.image setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

You can download it from gitHub

Epigrammatist answered 7/5, 2015 at 11:53 Comment(3)
I had used SDImage, never knew AF could do that too...ThanksEbenezer
can i able to use image url as placeholder image ?Teliospore
For any future devs, the import is no longer necessary.Diner
C
15

Try this code to get image...

Code:

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

NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];

[self.imageview1 setImage:image];    

Note: i have used test url. you can use your url.

Update : Swift 3.0 Code :

let url = URL(string: "http://www.fnordware.com/superpng/pnggrad16rgb.png")
    do {
        let  data = try Data(contentsOf: url!)
        var image = UIImage(data: data)
        self.imageView.image = image
        print(image)
    } catch {

    }

Note : In Xcode version 7.1 and above you need to set ATS (App Transport Security). To set ATS you need write below code in info.plist file. Make sure url of image should not be nil.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Cathryncathy answered 7/5, 2015 at 9:35 Comment(2)
Tested this in Playgrounds, converted to swift 3, and works perfectly! Thanks!Credible
@Credible Thanks for your feedback i have updated the answer and also added code for swift.Cathryncathy
E
13

Use AFnetworking where you can set placeholder image also if the url is not correct. This is more efficient when you are loading images in a tableview or a collection view.

Import UIImageView+AFNetworking.h

#import "UIImageView+AFNetworking.h"

and us the following code:

NSString *url1=@"http://www.fnordware.com/superpng/pnggrad16rgb.png";
[self.image setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

You can download it from gitHub

Epigrammatist answered 7/5, 2015 at 11:53 Comment(3)
I had used SDImage, never knew AF could do that too...ThanksEbenezer
can i able to use image url as placeholder image ?Teliospore
For any future devs, the import is no longer necessary.Diner
B
3

Check out AsyncImageView. Its the best async image loader AFAIK. all you have to do is set the image URL to the imageview and the rest is taken care of. including caching.

Buller answered 7/5, 2015 at 10:36 Comment(1)
this one is very easy to useMozell
H
1

I found the problem

My code is working correctly.

The problem was somewhere in my code , the ImageView was nil. Thanks all

Holle answered 7/5, 2015 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.