问题
We were using AFNetworking only to load images like this:
[thumbnailImage setImageWithURL:_item.thumbnailUrl];
and it worked fine. But now we've pulled in the rest of the project it has stopped working. It was just showing the background color. So I tried using this and it loads the placeholder but never loads the image.
UIImage* placeholder = [UIImage imageNamed:@"placeholder"];
[thumbnailImage setImageWithURL:_item.thumbnailUrl placeholderImage:placeholder];
I thought I might be able to see what the problem was so I tried:
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:_item.thumbnailUrl cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[thumbnailImage setImageWithURLRequest:urlRequest placeholderImage:[UIImage imageNamed:@"placeholder"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
NSLog(@"Loaded Image");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
NSLog(@"Failed to load Image");
}];
and it seems that it's not getting the return type that it's expecting. It says Expected content type image/png, got binary/octet-stream
I'm pulling the image from Amazon S3 so I don't know if I have control over the type.
回答1:
I wasn't able to change the data type so I overrode the acceptableContentTypes
method in AFNetworking by adding the following files to my project.
AFImageRequestOperation+OctetMIMEType.h
//
// AFImageRequestOperation+OctetMIMEType.h
// iQ.fileShare
//
// Created by Poole, Patrick on 4/25/13.
//
//
#import "AFImageRequestOperation.h"
@interface AFImageRequestOperation (OctetMIMEType)
@end
AFImageRequestOperation+OctetMIMEType.m
//
// AFImageRequestOperation+OctetMIMEType.m
// iQ.fileShare
//
// Created by Poole, Patrick on 4/25/13.
//
//
#import "AFImageRequestOperation+OctetMIMEType.h"
@implementation AFImageRequestOperation (OctetMIMEType)
+ (NSSet *)acceptableContentTypes {
return [NSSet setWithObjects:@"binary/octet-stream",@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon" @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil];
}
@end
回答2:
AFNetworking provides a method to add acceptable content types in 1.3.3, I'm not sure what other versions support this
[AFImageRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:@"binary/octet-stream", nil]];
来源:https://stackoverflow.com/questions/20153018/afnewtorking-loading-images-from-amazon-s3