Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download image with AFNetworking 2.0?

Appareantly there is no AFImageRequestOperation, but only AFImageResponseSerializer and frankly I don't get it or maybe I'm just looking too long through AFNetworking site... Downloading images with previous AFNetworking was like a charm. I'd hate to go back to older AFnetworking, since I did almost all stuff via the new version... Anyone?

like image 728
raistlin Avatar asked Oct 21 '13 18:10

raistlin


2 Answers

SO you want something like this for 2.0.

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];

As mentioned by Adam you can also do something like the below if you are just wanting to throw it into an imageView

[myImageView setImageWithURL:[NSURL URLWithString:@"http://sitewithimage.com/images/myimage.png"]];
like image 139
Bot Avatar answered Nov 16 '22 11:11

Bot


for old version, there is no responseSerializer, you can also

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

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Image error: %@", error);
}];
[requestOperation start];
like image 4
lbsweek Avatar answered Nov 16 '22 11:11

lbsweek