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?
Asked
Active
Viewed 3.7k times
51

raistlin
- 4,298
- 5
- 32
- 46
3 Answers
126
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"]];

Bot
- 11,868
- 11
- 75
- 131
-
Would this be on the main queue or the background queue? – Robert J. Clegg Feb 19 '14 at 13:34
-
@Tander The completion blocks run on the main queue – Bot Feb 19 '14 at 16:12
-
3This answer's great and all, but check out https://github.com/AFNetworking/AFNetworking/blob/master/UIKit%2BAFNetworking/UIImageView%2BAFNetworking.h, a lot of donkey work done for you if you're just putting an image in an image view – Adam Waite Feb 25 '14 at 00:00
-
@AdamWaite thanks, I updated my answer with a generic version, however if someone would like to use placeholders and such you can look at all the options in the link – Bot Feb 25 '14 at 01:10
-
1Bot's answer that the completion block is correct, though simplifying just a little. There's a `completionQueue` property on **AFURLConnectionOperation** that you can use to specify the queue used. The default is the main queue. :) – Steven Fisher Jun 30 '14 at 18:31
4
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];

andrewchan2022
- 4,953
- 45
- 48
0
For people using AFNetworking
in Swift, above solution can be written as below
let requestOperation : AFHTTPRequestOperation = AFHTTPRequestOperation(request: urlRequest)
requestOperation.responseSerializer = AFImageResponseSerializer()
requestOperation.setCompletionBlockWithSuccess({ (requestOperation, responseObject) in
print(responseObject)
_imageView.image = responseObject as? UIImage
}) { (requestOperation, error) in
print(error)
}
requestOperation.start()

Community
- 1
- 1

Penkey Suresh
- 5,816
- 3
- 36
- 55