AFNetworking 2.0,使用NSURLSession下载图像

我试图使用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;
}

是什么赋予了?


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

链接地址: http://www.djcxy.com/p/96769.html

上一篇: AFNetworking 2.0, downloading image with NSURLSession

下一篇: How to get download progress in AFNetworking 2.0?