使用AFNetworking后台上传大量图像

时间:2017-09-27 09:32:57

标签: ios objective-c afnetworking afnetworking-3

我正在寻找使用AFNetworking在后台上传大约100张图片,我是否需要创建一个操作队列?或者我可以在for循环中创建多个请求,如下所示:

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = nil;
    [manager.requestSerializer setTimeoutInterval:30.0];

    for(MyImageView *myImageView in images){
        NSDictionary *parameters = _PARAMETERS;
        [manager POST:@"//MY_URL_" parameters:parameters
        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:UIImageJPEGRepresentation(myImageView.image, 0.65) name:@"NAME" fileName:@"name" mimeType:@"image/jpeg"];
        }progress:^(NSProgress * _Nonnull uploadProgress) {

        } success:^(NSURLSessionTask *task, id responseObject) {
            NSDictionary *response = (NSDictionary *)responseObject;
            dispatch_async(dispatch_get_main_queue(), ^{
                int success = [[response objectForKey:@"success"] intValue];
                if(success == 1) {
                    NSLog(@"IMAGE UPLOAD SUCCESSFULL");
                }
                else if (success == 0){
                    NSLog(@"IMAGE UPLOAD FAILED");
                }
                NSLog(@"%@",response);
            });
        } failure:^(NSURLSessionTask *operation, NSError *error) {
            NSLog(@"%@",error.localizedDescription);
        }];
    }

如果需要,如何创建NSOperations队列?

由于

1 个答案:

答案 0 :(得分:0)

AFHTTPRequestOperation NSOperarationQueue NSOperationQueueDefaultMaxConcurrentOperationCount

一起使用