Download UIImage from URL in iOS

I am trying to load a UIImage from its url with the image completion and placeholder , but the UIImage and placeholder UIImage are not loading.

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) {} ]; 
+5
source share
4 answers

Use AFnetworking , where you can set the placeholder image if the URL is incorrect. This is more effective when loading images in a table or collection.

Import UIImageView+AFNetworking.h

 #import "UIImageView+AFNetworking.h" 

and we have 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

+11
source

Try this code to get the image ...

The 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 used the 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 install ATS (application transport security). To install ATS, you need to write the code below in the info.plist file. Make sure the image URL should not be zero.

 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 
+14
source

Check out AsyncImageView . Its best AFAIK asynchronous image downloader. all you have to do is set the image url to the image and the rest will take care. including caching.

+3
source

I found a problem

My code is working correctly.

The problem was somewhere in my code, ImageView was nil . Thank you all

+2
source

All Articles