AFNetworking 2.0 setImageWithURLRequest

故事扮演 提交于 2019-12-24 03:19:20

问题


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!


回答1:


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?




回答2:


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.




回答3:


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.




回答4:


https://github.com/AFNetworking/AFNetworking/archive/master.zip is including UIKit+AFNetworking directory



来源:https://stackoverflow.com/questions/19172543/afnetworking-2-0-setimagewithurlrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!