AFNetworking setimagewithurlrequest:placeholderImage always hit failure block - ios

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.

Related

AfNetworking image loading issue

I am having a little trouble implementing the AFNetworking 3.0 image loading method. This is the way I am trying to use it. Can anyone tell me what am I doing wrong?
[self.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.imageURL]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
self.imageView.image = image;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Unable to retrieve image");
}];
It is giving me compile time error which is:
No Visible #interface for 'UIImageView' declares the selector 'setImageWithURLRequest:placeHolder:success:failure
Import this file : UIImageView+AFNetworking.h.
This includes the method setImageWithURLRequest:placeHolder:success:failure method.

AFNetworking setImageWithURLRequest not updating UIImageView

I am downloading an image from a URL using setImageWithURLRequest:placeholderImage:success:failure and the download of the image works correctly. The issue I am having is that when I set the downloaded image to the UIIMageView nothing shows but the placeholder is removed.
My code is as follows:
__weak UIImageView *userCardImageView;
AFImageResponseSerializer *serializer = [[AFImageResponseSerializer alloc] init];
serializer.acceptableContentTypes = [serializer.acceptableContentTypes setByAddingObject:#"image/png"];
self.userCardImageView.imageResponseSerializer = serializer;
[self.userCardImageView setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[userCardImageView setImage:image];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
if (error)
{
NSLog(#"Error: %#", error);
NSLog(#"Error response: %#", response);
}
}];
The URL is correct as when I copy and past it into a browser I get my image.
Can someone advise me how I can get my code to work to update the URL? Am I right to assign the UIImageView to a weak property? I am doing this as using self or a strong variable could lead to a retain cycle.
It seems that you should implement the line
[userCardImageView setImage:image];
instead of
[userCardImageView setImage:[UIImage imageWithData:userCardImageData]];
in your success block
PS: If it is the self.userCardImageView which you want to set the image in, you would rather declare a weak controller instead of a weak UIImageView reference. Something like
__weak __typeof(&*self)weakSelf = self
Actually you are not assigning the downloaded image to image view correctly.[userCardImageView setImage:[UIImage imageWithData:userCardImageData]];, we need not to assign the image from this variable userCardImageData, try this code
[self.userCardImageView setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
dispatch_async(dispatch_get_main_queue(), ^{
[userCardImageView setImage:image]; //set image in main thread , we got downloaded image as parameter in block
});
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
if (error)
{
NSLog(#"Error: %#", error);
NSLog(#"Error response: %#", response);
}
}];
Hope this helps
Should be enough just:
[self.userCardImageView setImageWithURL:url];

Set a downloaded image to Imageview from url - IoS

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.)

AFNetworking and strange error -999 [duplicate]

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:#""];

AFNetworing - setImageWithURLRequest not working

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:#""];

Resources