AFNetworking 2.0,使用NSURLSession下载图像

时间:2014-01-17 13:22:28

标签: ios cocoa-touch ios7 nsurlconnection afnetworking-2

我正在尝试使用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 个答案:

答案 0 :(得分:0)

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