2

In ios using AFNetworking 2.0 how can I easily download a remote image asynchronously and cache it for future request of the same url? I'm looking for a convenient way to receive both error callbacks and successful ones.

Thanks

Johan Falk
  • 4,341
  • 2
  • 30
  • 42
vondip
  • 13,809
  • 27
  • 100
  • 156

1 Answers1

19

You can do

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Response: %@", responseObject);
    _imageView.image = responseObject;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Image error: %@", error);
}];
[requestOperation start];

also mentioned How to download image with AFNetworking 2.0?

Community
  • 1
  • 1
Bot
  • 11,868
  • 11
  • 75
  • 131
  • AFHTTPRequestOperation doesn't have a member called responseSerializer. Should use AFHTTPRequestOperationManager instead. – Andy Ibanez Sep 16 '14 at 03:34
  • I have used both 2.0 and 2.4.1 and none of them seem to have this property. – Andy Ibanez Sep 16 '14 at 17:15
  • @AndyIbanez not sure what is going on with your project then. https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-2.0-Migration-Guide look under __Serialization__ – Bot Sep 16 '14 at 18:40
  • It must have have something to do with the fact I added the project from CocoaPods. I guess I will just try to download it directly from GitHub. – Andy Ibanez Sep 16 '14 at 18:56
  • @AndyIbanez make sure you have the correct `#import` – Bot Sep 16 '14 at 20:09