ios – AFNetworking 2.2.1从Amazon S3服务器加载图像

前端之家收集整理的这篇文章主要介绍了ios – AFNetworking 2.2.1从Amazon S3服务器加载图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在尝试在Amazon S3服务器上下载图像时遇到了问题.

我收到以下错误

Error Domain=AFNetworkingErrorDomain Code=-1016 "Request Failed: unacceptable content-type: binary/octet-stream"

有人有想法吗?

解决方法

错误生成
- (BOOL)validateResponse:(NSHTTPURLResponse *)response
                    data:(NSData *)data
                   error:(NSError * __autoreleasing *)error

在不可思议的MIME类型的响应的情况下AFHTTPResponseSerializer的方法.

您可以通过向响应序列化程序添加必需的MIME类型来修复它

// In this sample self is inherited from AFHTTPSessionManager
self.responseSerializer = [AFImageResponseSerializer serializer];
NSSet *set = self.responseSerializer.acceptableContentTypes;
self.responseSerializer.acceptableContentTypes = [set setByAddingObject:@"binary/octet-stream"];

或者您可以修改AFImageResponseSerializer:

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"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",@"binary/octet-stream",nil];

#if defined(__IPHONE_OS_VERSION_MIN_required)
    self.imageScale = [[UIScreen mainScreen] scale];
    self.automaticallyInflatesResponseImage = YES;
#endif

    return self;
}

但问题的根源可能是您使用错误的MIME类型或根本没有类型将图像保存到亚马逊.在我的代码中,我使用以下代码将图像保存到亚马逊

S3PutObjectRequest *putObjectRequest = [ [ S3PutObjectRequest alloc ] initWithKey:keyImage    inBucket:self.s3BucketName ];
putObjectRequest.contentType = @"image/jpeg";
putObjectRequest.data = UIImageJPEGRepresentation( [ image fixOrientation ],0.5f );
putObjectRequest.cannedACL = [ S3CannedACL publicRead ];

猜你在找的iOS相关文章