userG
userG

Reputation: 111

AFNetworking 3.0 AFURLResponseSerialization missing

I was assigned to work on a project that was using afnetworking which was added manually. I removed it and installed AFNetworking 3.0 through cocoaPods.

A number of things are broken. I was manually assigning the acceptable content type as shown below:

AFImageResponseSerializer *serializer = [[AFImageResponseSerializer alloc] init];


serializer.acceptableContentTypes = [serializer.acceptableContentTypes setByAddingObject:@"application/x-www-form-urlencoded"];

self.MyCollectionViewCell.uiButton.imageView.imageResponseSerializer = serializer;

[self.MyCollectionViewCell.uibutton.imageView setImageWithURLRequest:request placeholderImage:nil
                                                                                 success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
 NSLog(@"Loaded successfully: %ld", (long)[response statusCode]);

[self.MyCollectionViewCell.uibutton setImage:image forState:UIControlStateNormal];

                                                                                 } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
    NSLog(@"failed loading: %@", error);

                                                                                 }];

In AfNetworking version 3 there is no such property

@property (nonatomic, strong) id <AFURLResponseSerialization> imageResponseSerializer;

I have been searching for an equivalent in version 3.0. Or is there any other way to get around the issue of manually setting the acceptable content type. I will be very thankful if someone could help me with this.

Upvotes: 3

Views: 1162

Answers (1)

Hong Duan
Hong Duan

Reputation: 4294

The API has changed in the 3.x version of AFNetworking.

Now the UIImageView+AFNetworking use a AFImageDownloader instance to manage the image download tasks, and AFImageDownloader use a AFHTTPSessionManager instance to manage the http request, so you can assign your custom AFImageResponseSerializer through AFImageDownloader like this:

AFImageResponseSerializer *serializer = [[AFImageResponseSerializer alloc] init];
serializer.acceptableContentTypes = [serializer.acceptableContentTypes setByAddingObject:@"application/x-www-form-urlencoded"];

AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:defaultConfiguration];
sessionManager.responseSerializer = serializer;

AFImageDownloader *imageDownloader = [[AFImageDownloader alloc] 
             initWithSessionManager:sessionManager
             downloadPrioritization:AFImageDownloadPrioritizationFIFO
             maximumActiveDownloads:4
                         imageCache:[[AFAutoPurgingImageCache alloc] init]];
[UIImageView setSharedImageDownloader:imageDownloader];

Upvotes: 2

Related Questions