AFNetworking 2 setImageWithURL custom response type image/jpeg
Asked Answered
M

4

5

Im using AFNetworking 2. I have a UITableview and each row contains an image.

The issue is that the response type is image/pjpeg which is not an accepted type by default. To get around this I have modified AFURLResponseSerialization.m around line 599. Adding this content type to the end of the self.acceptableContentTypes declaration.

I would prefer not to modify the source. Is there a proper way to do this in 2.x?

NSString *url = [NSString stringWithFormat:@"%@my/images/%@",BaseUrl,[o objectForKey:@"ID"]];
[cell.imageView setImageWithURL:[NSURL URLWithString:url]
               placeholderImage:[UIImage imageNamed:@"placeholder"]
 ];

This no longer seems to work:

[AFImageRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"image/jpeg"]]

Update:

I can see the error using the following code:

   NSURLRequest *urlRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: url]];

__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:urlRequest
                      placeholderImage:[UIImage imageNamed:@"placeholder"]
                        success: ^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                            __strong UITableViewCell *strongCell = weakCell;

                            strongCell.imageView.image = image;

                            [tableView reloadRowsAtIndexPaths: @[indexPath]
                                             withRowAnimation: UITableViewRowAnimationNone];
                            NSLog(@"Your image request succeeded!");
                        } failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                            NSLog(@"Your image request failed...");

                            NSLog(@"Error: %@", error);
                            NSLog(@"Error: %@", response);
                        }

 ];

Here is the error:

Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: image/pjpeg"
Millwater answered 30/1, 2014 at 20:16 Comment(3)
You probably don't need to change the code neither subclass. In fact "acceptableContentTypes" is a property of AFHTTPResponseSerializer which is a super class of AFImageResponseSerializer. So once you have created the AFImageResponseSerializer, didn't you try to simply set the new value for acceptableContentTypes? you can append the new content type to the original using [serializer setAcceptableContentTypes:[serializer.acceptableContentTypes setByAddingObject:@"image/jpeg"]]; I didn't test this, I just guessed looking at the code. Did you try this solution?Europium
I don't create a AFImageResponseSerializer, the code above is all I have. I don't know how to modify the serializer when I'm doing a setImageWithURL (I do know how to with a normal request like AFHTTPRequestOperationManager).Millwater
See the response from David Caunt below.Europium
D
13

You can set your own imageResponseSerializer on a UIImageView instance:

AFImageResponseSerializer *serializer = [[AFImageResponseSerializer alloc] init];
serializer.acceptableContentTypes = [serializer.acceptableContentTypes setByAddingObject:@"image/pjpeg"];
cell.imageView.imageResponseSerializer = serializer;

NSString *url = [NSString stringWithFormat:@"%@my/images/%@",BaseUrl,[o objectForKey:@"ID"]];
[cell.imageView setImageWithURL:[NSURL URLWithString:url]
               placeholderImage:[UIImage imageNamed:@"placeholder"]
];
Dariodariole answered 30/1, 2014 at 20:58 Comment(8)
I just tried that, did work for me. How does setImageWithURL know to use serializer?Millwater
You can see it here and here - the serializer is associated with the image view and then used when a response is received from the network.Dariodariole
Im still getting the error, I updated the question with additional code.Millwater
Sorry, I made an edit. You need to set the serializer on the imageView, of course. Does this work for you now?Dariodariole
Perfect. That works now, was wondering how they were associated.Millwater
Is there a way to do this once for all image requests? I really liked that in a v1 approach.Nonego
There's no way with AFNetworking. One way would be to use your own UIImageView subclass which sets the serializer. Alternatively you could add your own category to load which sets the serializer then calls one of the setImageWithURL methods.Dariodariole
@DavidCaunt I found a way, although hacky one. I posted it as an answer.Nonego
N
2

I found a way to set accepted MIME types once for image serialisation, albeit it is a bit hacky. UIImageView+AFNetworking provides users with a default AFImageResponseSerializer, and it turns out the default instance is shared across all UIImageView instances, unless a custom serialiser was set.

Executing this code at app launch will modify default behaviour of all image views:

AFImageResponseSerializer *serializer =
    [[[UIImageView alloc] init] imageResponseSerializer];
NSSet *mimeTypes = [serializer.acceptableContentTypes 
    setByAddingObjectsFromArray:@[@"image/pjpeg", @"image/x-png"]];
[serializer setAcceptableContentTypes:mimeTypes];

This approach works perfectly for me, but beware that this doesn't rely on any public contract and may change in future versions. If you choose to use this, cover this with unit tests which ensure that instance is indeed shared.

The approach will also break if you execute something like

imageView.imageResponseSerializer = [[AFImageResponseSerializer alloc] init];

as it will replace the shared instance with the unmodified default behaviour.

Nonego answered 30/5, 2014 at 17:11 Comment(0)
A
2

For those using AFNetworking 3.0, you can simply update responseSerializer on the shared sessionManager.

AFImageResponseSerializer* serializer = (AFImageResponseSerializer*)      [UIImageView sharedImageDownloader].sessionManager.responseSerializer;
serializer.acceptableContentTypes = [serializer.acceptableContentTypes setByAddingObject:@"image/jpg"];
Anaphylaxis answered 18/2, 2017 at 14:40 Comment(0)
W
1

if you are using RestKit or AFNetworking 1.3 use

[AFImageRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"image/pjpeg"]];
Winson answered 17/11, 2015 at 4:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.