I am unable to download the images, The setImageWithURLRequest method is not going inside the success/failure block. Please someone advise me how solve this task. This is my code.
__weak UIImageView *images ;
NSString *url =[NSString stringWithFormat:#"%#%#",imageBaseUrl,eachImage];
NSLog(#"image download url %#",url);
[images setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if(response){
[arr addObject:image];
NSLog(#"Success fetching image");
}else{
NSLog(#"The image data is not there");
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Request failed with error: %#", error);
}];
I have checked above code and it is worked for other image URL. While i am used your image URL it is failed to load image.
if you add manually http:// to URL. Then image is load properly.
Your URL should be like http://3.0.191.16/uploads/patient/2133/38049.jpg
Your logic should be like below.
if url doesn't contain http then append http:// manually otherwise go with url from server.
Or you can ask API developer to send proper image URL.
Please mark as accept if it will work for you.
Related
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 am trying to set image in UICollectionViewCell, via collectionView:cellForItemAtIndexPath:. I am using AFNetworking Image Category UIImageView+AFNetworking.h method for loading image from cloud server.
The problem is that, I am always hitting failure block. When open the link in browser window, I can see the image.
Here is the sample code:
__weak UICollectionViewCell *blockcell = cell;
NSURL *imageUrl = [NSURL URLWithString:#"https://ec2-75-101-163-253.compute-1.amazonaws.com/static/icons/app-excel%402x.png"];
[cell.appImage setImageWithURLRequest:[NSURLRequest requestWithURL:imageUrl] placeholderImage:[UIImage imageNamed:#"file-gray.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
blockcell.appImage.image = image;
[blockcell setNeedsLayout];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"fail for %#",request.URL);
}];
There might be a simple/silly mistake. Can anyone help me.
Thanks in advance.
I want to set tableview cell images in one of my apps. I use AFNetworking (1.2.1) and I do it with setImageWithURLRequest.
My code in which I set images looks like this:
if (user.avatar) {
NSString *imageUrl = user.avatar.url;
[cell.profileImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[cell setNeedsLayout];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
My error log returns this:
Error: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x1f5b56a0 {NSErrorFailingURLKey=https://d2rfichhc2fb9n.cloudfront.net/image/5/Cf7T_BA6- NFGL5ah0w4hdvmk_Vp7InMiOiJzMyIsImIiOiJhZG4tdXNlci1hc3NldHMiLCJrIjoiYXNzZXRzL3VzZXIvZTIvM2MvMjA vZTIzYzIwMDAwMDAwMDAwMC5qcGciLCJvIjoiIn0}
And the images dosn't get set. I didn't have this problem before I updated to AFNetworking (1.2.1). Any ideas?
Update
Tried to strip the whitespace from the url like this:
NSString *imageUrl = [user.avatar.url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
But I get the same error. And it could maybe be dangerous if the API sometime would contain a image file name that contains whitespaces.
I see three problems:
-999 means NSURLErrorCancelled, so it looks like the URLRequest gets cancelled somehow. Where is this code of yours? Is it possible that the cell object or the UIImageView profileImage is released prematurely?
More recent versions of AFNetworking require you to set the image yourself if you're defining a success block.
Make sure the URL is valid.
Try this (works when placed in tableView:cellForRowAtIndexPath:):
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if (weakCell)
{
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Error: %#", error);
}];
There seem to be some extra space in your url
Your url is https://d2rfichhc2fb9n.cloudfront.net/image/5/Cf7T_BA6- NFGL5ah0w4hdvmk_Vp7InMiOiJzMyIsImIiOiJhZG4tdXNlci1hc3NldHMiLCJrIjoiYXNzZXRzL3VzZXIvZTIvM2MvMjA vZTIzYzIwMDAwMDAwMDAwMC5qcGciLCJvIjoiIn0
Url Without Space
Because of the extra space the image url is invalid and its not getting fetched.
EDIT
NSString *strCorrectUrl = [user.avatar.url stringByReplacingOccurrencesOfString:#" "
withString:#""];
I want to set tableview cell images in one of my apps. I use AFNetworking (1.2.1) and I do it with setImageWithURLRequest.
My code in which I set images looks like this:
if (user.avatar) {
NSString *imageUrl = user.avatar.url;
[cell.profileImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[cell setNeedsLayout];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
My error log returns this:
Error: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x1f5b56a0 {NSErrorFailingURLKey=https://d2rfichhc2fb9n.cloudfront.net/image/5/Cf7T_BA6- NFGL5ah0w4hdvmk_Vp7InMiOiJzMyIsImIiOiJhZG4tdXNlci1hc3NldHMiLCJrIjoiYXNzZXRzL3VzZXIvZTIvM2MvMjA vZTIzYzIwMDAwMDAwMDAwMC5qcGciLCJvIjoiIn0}
And the images dosn't get set. I didn't have this problem before I updated to AFNetworking (1.2.1). Any ideas?
Update
Tried to strip the whitespace from the url like this:
NSString *imageUrl = [user.avatar.url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
But I get the same error. And it could maybe be dangerous if the API sometime would contain a image file name that contains whitespaces.
I see three problems:
-999 means NSURLErrorCancelled, so it looks like the URLRequest gets cancelled somehow. Where is this code of yours? Is it possible that the cell object or the UIImageView profileImage is released prematurely?
More recent versions of AFNetworking require you to set the image yourself if you're defining a success block.
Make sure the URL is valid.
Try this (works when placed in tableView:cellForRowAtIndexPath:):
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if (weakCell)
{
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Error: %#", error);
}];
There seem to be some extra space in your url
Your url is https://d2rfichhc2fb9n.cloudfront.net/image/5/Cf7T_BA6- NFGL5ah0w4hdvmk_Vp7InMiOiJzMyIsImIiOiJhZG4tdXNlci1hc3NldHMiLCJrIjoiYXNzZXRzL3VzZXIvZTIvM2MvMjA vZTIzYzIwMDAwMDAwMDAwMC5qcGciLCJvIjoiIn0
Url Without Space
Because of the extra space the image url is invalid and its not getting fetched.
EDIT
NSString *strCorrectUrl = [user.avatar.url stringByReplacingOccurrencesOfString:#" "
withString:#""];
I'm using AFNetworking category UIImageView+AFNetworking.h in order to download an image from S3. I get a pre-signed URLRequest and proceed to get the image. This is in a controller solely meant to display a photo:
- (void)viewWillAppear:(BOOL)animated
{
NSURLRequest *request = [BFAWSUploader downloadPhotoWithFileName:[NSString stringWithFormat:#"%#.jpg", _topicMessage.attachment_s3_name]];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
[_imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:#"Default.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(#"Pulled down image successfully");
[hud hide:YES];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Failed to pull down image");
}];
}
I see the log entry "Pulled down image successfully", but I'm not seeing the image view update after that. Instead, I have to leave the view controller and then go back in, and I'll see the cached entry.
From UIImageView+AFNetworking.h:
"If a success block is specified, it is the responsibility of the block to set the image of the image view before returning. If no success block is specified, the default behavior of setting the image with self.image = image is executed."
So, you need _imageView.image = image inside your success block.