I am trying to load a UIImage
from its URL with completion
and 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) {}
];
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.I found the problem
The problem was somewhere in my code , the
ImageView
wasnil
. Thanks allUse
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
and us the following code:
You can download it from gitHub
Try this code to get image...
Code:
Note: i have used test url. you can use your url.
Update : Swift 3.0 Code :
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.