Image view not updating after fetching image from URL - ios

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.

Related

AFNetworking setImageWithURLRequest not working

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.

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 setimagewithurlrequest:placeholderImage always hit failure block

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.

AFNetworking+UIImageView placeholder image shows up, but not URL image

I'm trying to use the AFNetworking UIImageView call to load images from a URL as shown below:
[self.image setImageWithURL:[NSURL URLWithString:feed.imageURL] placeholderImage: [UIImage imageNamed:#"logo"]];
The placeholder image always shows up, but the actual image from "feed.imageURL" never does. I've verified that the URL is actually correct. I even hardcoded it to make sure, and still nothing.
My basic app setup is a tab controller...and in viewDidLoad, I call a method "fetchFeed" which performs the HTTP request to gather my JSON data.
My request block looks like:
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[self parseDictionary:JSON];
isLoading = NO;
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Error: %#", error);
[self showNetworkError];
isLoading = NO;
[self.tableView reloadData];
}];
operation.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript", #"text/html", nil];
[queue addOperation:operation];
Turns out the server I was requesting the image from was sending content-type "image/jpg" and by default AFNetworking does not support this file type.
I changed the class method in AFImageRequestOperation to look like:
+ (NSSet *)defaultAcceptableContentTypes {
return [NSSet setWithObjects:#"image/tiff", #"image/jpeg", #"image/gif", #"image/png", #"image/ico", #"image/x-icon" #"image/bmp", #"image/x-bmp", #"image/x-xbitmap", #"image/x-win-bitmap", #"image/jpg", nil];
}
and it fixed my problem.
You can manage to accept what content-type you want with this library simply changing the request like this:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:yourURL];
[request addValue:#"image/*" forHTTPHeaderField:#"Accept"];
And call the AFNetworking method:
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
This way you will be able to override the content-type without changing the library.
AFNetworking doesn't support image/jpg MIME TYPE by default.
You can support it without modifying the AFNetworking Library
[AFImageRequestOperation addAcceptableContentType:#"image/jpg"];
All operations that manipulate the UI must be performed on the main thread. So you may need to use 'performSelectorOnMainThread:' when reloading your tableview data in the completion block.
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO]
I had a similar problem but it turned out that I was passing a URL which contained spaces in it. When I properly encoded the URL using stringByAddingPercentEscapesUsingEncoding: the images now load.

Resources