AFNetworking 2.0 setImageWithURLRequest [英] AFNetworking 2.0 setImageWithURLRequest
本文介绍了AFNetworking 2.0 setImageWithURLRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我当前的一个项目中下载图像时,我正在使用此代码,但它不适用于AFNetworking 2.0。我尝试通过AFImageResponseSerializer进行尝试,但找不到正确的代码。
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);
}];
对AFNetworking 2.0中使用的新代码有何建议?谢谢!
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?
这篇关于AFNetworking 2.0 setImageWithURLRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文