AFNetworking 2.0, downloading image with NSURLSession

I'm trying to download an image from a secure server, using AFNetworking 2.0 and NSURLSession. However, I get a bytestream that cannot be converted into an image if I try to download like this:

- (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;
}

Just worth mentioning, it works perfectly if I try using this:

- (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;
}

What gives?


The problem was actually with the images coming from the server, even if the browser can open them. The png tags were corrupt

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

上一篇: AFNetworking和后台传输

下一篇: AFNetworking 2.0,使用NSURLSession下载图像