AFNetworking 2 setImageWithURL自定义响应类型image/jpeg

时间:2021-10-14 21:20:14

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

我使用AFNetworking 2。我有一个UITableview,每一行都包含一个图像。

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.

问题是响应类型是image/pjpeg,默认情况下这不是一个可接受的类型。为了解决这个问题,我修改了反褶积。在第599行。将此内容类型添加到self的末尾。acceptableContentTypes声明。

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

我宁愿不修改源代码。在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"

4 个解决方案

#1


13  

You can set your own imageResponseSerializer on a UIImageView instance:

您可以在UIImageView实例上设置自己的imageResponseSerializer:

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

#2


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.

我找到了一种方法,可以为图像序列化设置一个被接受的MIME类型,尽管它有点粗糙。UIImageView+AFNetworking为用户提供一个默认的AFImageResponseSerializer,结果显示,默认实例在所有UIImageView实例之间共享,除非设置了自定义的serialiser。

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

在app launch中执行此代码将修改所有图像视图的默认行为:

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.

因为它将用未修改的默认行为替换共享实例。

#3


1  

if you are using RestKit or AFNetworking 1.3 use

如果您正在使用RestKit或AFNetworking 1.3使用

[AFImageRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"image/pjpeg"]];

#4


1  

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

对于使用AFNetworking 3.0的用户,您可以简单地更新共享sessionManager上的responseSerializer。

AFImageResponseSerializer* serializer = (AFImageResponseSerializer*)      [UIImageView sharedImageDownloader].sessionManager.responseSerializer;
serializer.acceptableContentTypes = [serializer.acceptableContentTypes setByAddingObject:@"image/jpg"];

#1


13  

You can set your own imageResponseSerializer on a UIImageView instance:

您可以在UIImageView实例上设置自己的imageResponseSerializer:

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

#2


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.

我找到了一种方法,可以为图像序列化设置一个被接受的MIME类型,尽管它有点粗糙。UIImageView+AFNetworking为用户提供一个默认的AFImageResponseSerializer,结果显示,默认实例在所有UIImageView实例之间共享,除非设置了自定义的serialiser。

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

在app launch中执行此代码将修改所有图像视图的默认行为:

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.

因为它将用未修改的默认行为替换共享实例。

#3


1  

if you are using RestKit or AFNetworking 1.3 use

如果您正在使用RestKit或AFNetworking 1.3使用

[AFImageRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"image/pjpeg"]];

#4


1  

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

对于使用AFNetworking 3.0的用户,您可以简单地更新共享sessionManager上的responseSerializer。

AFImageResponseSerializer* serializer = (AFImageResponseSerializer*)      [UIImageView sharedImageDownloader].sessionManager.responseSerializer;
serializer.acceptableContentTypes = [serializer.acceptableContentTypes setByAddingObject:@"image/jpg"];