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) {}
];
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>
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
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
My code is working correctly.
The problem was somewhere in my code , the ImageView was nil.
Thanks all
Related
I am setting image in UIImageView using:
[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:#"random_image"]];
I am able to see the image on phone as expected.
However, if I use the method without placeholder argument
[self.imageView setImageWithURL:[NSURL URLWithString:urlString]];
OR pass the placeholderImage as nil
[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];
then the image is not set and my imageView is blank. I understand that placeholder image is for "representing the current state of the asset suitable for temporarily displaying in your extension’s UI.".
I don't understand how its absence should impact me the way it is impacting. Any pointers?
If you set default placeholder image then imageview gets width and night.Otherwise imageview frame is may be (0,0).
Or
The image should be downloaded asynchronously using NSURLConnection, and only once it's fully downloaded should it be converted into a UIImage and assigned to the image view
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
ImageViewName.image = [UIImage imageWithData:data];
}];
I found the issue. I added an image view in UITableViewCell with the name imageView. Apparently, UITableViewCell already has a property with the same name. I changed the name of myImageView to cellImageView and now, I don't need the placeholder parameter anymore! Having said that, I still don't understand what happened behind the screens!
i hope these code will help u....
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:#“image URL”]]];
dispatch_sync(dispatch_get_main_queue(), ^{
[[cell myimage] setImage:image];
}
});
});
I'm working with AFNetworking to load my images for cell placeholder. Here is my code example. (Token from here)
NSURL *url = [NSURL URLWithString:daysWeather.weatherIconURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIImage *placeholderImage = [UIImage imageNamed:#"placeholder"];
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:request
placeholderImage:placeholderImage
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(#"Load placeholder img");
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
} failure:nil];
return cell;
And now I have a question. Does this images load every time, when it becomes visible? As you can see I've added NSLog(#"Load placeholder img"); in success block. And I see, that this code calls every time, when cell becomes visible (after it was visible, disappeared and become visible again)
Yes success block called every time. But AFNetworking will download the image only once and saved it in cache. Once you make request again for same URL or image it will directly load image from cache if available.
Also, if you don't want to do any specific operation in block, then simply use setImageWithURL:placeholderImage.
I have an url, clicking which it downloads the image. I want to set the image to the imageview. Currently have tried this but the image does not load. Can anyone suggest where i am going wrong.
Thanks in advance!
NSURL *url1 = [NSURL URLWithString:#"http://farm4.staticflickr.com/3695/9258420948_566a38bfdb_q_d.jpg"];
pic1.image = [UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:url1]];
You want to use AFNetworking's category on UIImageView:
[myImageView setImageWithURLRequest:urlRequest placeholderImage:[UIImage imageNamed:#"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
myImageView.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Request failed with error: %#", error);
}];
It is worth working with AFNetworking just for this call. (AFNetworking also gives you a placeholder image, and it also caches network images so subsequent calls use the cache giving you a quick response.)
I Have copied the contents of file from here and created a UIImageView+AFNetworking.h and imported in my implementation file
Now When I Write the following code i get this error but when i remove the block of code then everything works fine.
I want to display image in a custom table cell.The url of image i am grabbing Through JSON
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[dictArray objectForKey:#"image"]]];
[cell.thumbnailImageView setImageWithURLRequest:imageRequest placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
NSLog(#"success");
cell.thumbnailImageView.image = image;
cell.thumbnailImageView.contentMode = UIViewContentModeScaleAspectFit;
cell.thumbnailImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell setNeedsLayout];// To update the cell {if not using this, your image is not showing over cell.}
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(#"Failure");}];
Here is the screen shot of the error
It Crashes after loading
Instead of using afnetworking You can just use dispatch method
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *url = [indexDic objectForKey:#"image"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
cellImg.image = image;
});
});
return cell;
}
i need download image from url and load it into UIIamge view
first i get image from URL , but data return with nil i dont know why ?
-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;
// data here return with nil
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
You can use AFNetworking
And simply use: - (void)setImageWithURL:(NSURL *)url;
Or something more complex:
UIImageView image;
NSString *urlstring = [NSString stringWithFormat:#"http://url/"];
NSURL *url = [NSURL URLWithString:urlstring];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[image setImageWithURLRequest:request
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
// Succes on loading image
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
// Fail to load image
}];
You can set your image from URL link like this:
NSString *your_url = #"http://your_link/";
image_View.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:your_url]]];
Also refer :
1) question 1
2) question 2
3) question 3