I'm using this code when downloading images in one of my current projects and it's not working with AFNetworking 2.0. I tried going thru AFImageResponseSerializer but I can't find the right code to use.
[cell.posterImage setImageWithURLRequest:urlRequest placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
cell.posterImage.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Request failed with error: %@", error);
}];
Any suggestions on the new code used in AFNetworking 2.0? Thanks!
I made it work using this code:
AFHTTPRequestOperation *posterOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
posterOperation.responseSerializer = [AFImageResponseSerializer serializer];
[posterOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
_posterImageView.image = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image request failed with error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:posterOperation];
[posterOperation start];
But I got into another problem with placeholder images using this. Any ideas?
You're looking for UIImageView (AFNetworking)
category.
#import "UIImageView+AFNetworking.h"
//...
[cell.posterImage setImageWithURLRequest:urlRequest placeholderImage:[UIImage imageNamed:@"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
cell.posterImage.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Request failed with error: %@", error);
}];
cell.posterImage
must be an UIImageView
not an UIImage
.
With the updated AFNetworking 2.0
(in case your files are accessible for download-only basis, so are not accessible by a simple URL) :
NSString * methodURL = @"http://www.yourserver.com/pathToImageDownloadMethod";
NSInteger someIDYouHaveOfAnImage = 13;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFImageResponseSerializer serializer];
NSDictionary * parameterDictionary = @{@"Parameter" : [NSNumber numberWithInteger:someIDYouHaveOfAnImage]};
[manager GET:methodURL parameters:parameterDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([responseObject isKindOfClass:[UIImage class]]) {
UIImage * image = responseObject;
// Here is your image object
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure getting the file : %@", error.localizedDescription);
}];
Hope it helps.
https://github.com/AFNetworking/AFNetworking/archive/master.zip is including UIKit+AFNetworking directory