AFNetworking 2.0,使用 NSURLSession 下载图片

Posted

技术标签:

【中文标题】AFNetworking 2.0,使用 NSURLSession 下载图片【英文标题】:AFNetworking 2.0, downloading image with NSURLSession 【发布时间】:2014-01-17 13:22:27 【问题描述】:

我正在尝试使用 AFNetworking 2.0 和 NSURLSession 从安全服务器下载图像。但是,如果我尝试像这样下载,我会得到一个无法转换为图像的字节流:

- (SLRequestExecutionBlock) _getExecutionBlockSession

    __block SLBaseRequest *blockSelf = self;
    SLRequestExecutionBlock executionBlock = ^

        AFHTTPSessionManager *imageDownloadManager = [AFHTTPSessionManager manager];
        [imageDownloadManager setResponseSerializer:[AFImageResponseSerializer serializer]];
        [imageDownloadManager setRequestSerializer:[AFJSONRequestSerializer serializer]];

        [imageDownloadManager.requestSerializer setValue:@"Accept" forHTTPHeaderField:@"application/json"];
        if ([SLLoginManager sharedManager].accessToken) 
            [imageDownloadManager.requestSerializer setValue:[SLLoginManager sharedManager].accessToken forHTTPHeaderField:@"access_token"];
        

        imageDownloadManager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
        imageDownloadManager.securityPolicy.allowInvalidCertificates = YES;

        NSDictionary *params = nil;

        [[SLHttpNetworkManager sharedManager] GET:_imageURL
                                       parameters:params
                                          success:blockSelf.standardSuccessBlock
                                          failure:blockSelf.standardErrorBlock];
    ;

    return executionBlock;

值得一提,如果我尝试使用它,它会完美运行:

- (SLRequestExecutionBlock) _getExecutionBlock

    __block SLBaseRequest *blockSelf = self;
    SLRequestExecutionBlock executionBlock = ^

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
        manager.securityPolicy.allowInvalidCertificates = YES;

        manager.responseSerializer = [AFImageResponseSerializer serializer];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        if ([SLLoginManager sharedManager].accessToken) 
            [manager.requestSerializer setValue:[SLLoginManager sharedManager].accessToken forHTTPHeaderField:@"access_token"];
        

        [manager GET:_imageURL
          parameters:nil
             success:^(AFHTTPRequestOperation *operation, id responseObject) 
                 blockSelf.returnAttributes = responseObject;
                 if (blockSelf.completionBlock) 
                     blockSelf.completionBlock(blockSelf.returnAttributes,nil);
                 
                 [[SLNetworkQueueManager sharedManager] requestDidSucceed:blockSelf];
              failure:^(AFHTTPRequestOperation *operation, NSError *error) 
                 //check error code for network errors
                 NSError* err = error;
                 [blockSelf failOrTryRefreshTokenForError:err];

             ];
    ;

    return executionBlock;

什么给了?

【问题讨论】:

【参考方案1】:

问题实际上是来自服务器的图像,即使浏览器可以打开它们。 png 标签已损坏

【讨论】:

以上是关于AFNetworking 2.0,使用 NSURLSession 下载图片的主要内容,如果未能解决你的问题,请参考以下文章

PHP REST 服务中的 JSON 格式问题从 AFNetworking 2.0 接收 POST

如何在旧的 AFNetworking 中使用 AFNetworking 2.0+?

AfNetworking 2.0 发布问题

AFNetworking 2.0 - 是不是使用子类?

AFNetworking 2.0,使用 NSURLSession 下载图片

使用 AFNetworking 2.0 加载图像