AFNetworking 2.0 setImageWithURLRequest

2019-07-21 07:05发布

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!

4条回答
对你真心纯属浪费
2楼-- · 2019-07-21 07:42

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?

查看更多
乱世女痞
3楼-- · 2019-07-21 07:47
\"骚年 ilove
4楼-- · 2019-07-21 07:51

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.

查看更多
家丑人穷心不美
5楼-- · 2019-07-21 08:02

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.

查看更多
登录 后发表回答