[AFNetworking练习2]GCD方式Post提交json

[_AllUpLoadInfos enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, UIImage * _Nonnull obj, BOOL * _Nonnull stop) {
        dispatch_async(QueueTask, ^{
            /*POST上传图片*/
            NSData* ImageData = UIImageJPEGRepresentation(obj, 1.0);
            NSURL* BaseUrlImg = [NSURL URLWithString:@"http://10.2.108.12:8086/"];
            NSString* ImageStr = [ImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            NSURLSessionConfiguration* Configur=[NSURLSessionConfiguration defaultSessionConfiguration];
            [Configur setTimeoutIntervalForRequest:3];
            AFHTTPSessionManager* ManagerImg = [[AFHTTPSessionManager alloc]
                                                initWithBaseURL:BaseUrlImg
                                                sessionConfiguration:Configur];
            NSMutableDictionary* ParamImg = [NSMutableDictionary dictionaryWithCapacity:3];
            ManagerImg.requestSerializer  = [AFJSONRequestSerializer  serializer];
            ManagerImg.responseSerializer = [AFJSONResponseSerializer serializer];
            ManagerImg.responseSerializer.acceptableContentTypes =[NSSet setWithObject:@"text/html"];
            [ParamImg setObject:[NSString stringWithFormat:@"%@.jpg",key] forKey:@"FileName"];
            [ParamImg setObject:MyNumber forKey:@"UserName"];
            [ParamImg setObject:ImageStr forKey:@"FileData"];
            [ManagerImg POST:@"/TestJSONData" parameters:ParamImg progress:^(NSProgress * _Nonnull uploadProgress) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self UpOneImageUpdate:key Complete:uploadProgress.fractionCompleted];
                });
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                dispatch_async(dispatch_get_main_queue(),^{
                    [self CompleteImageUpdate:key];
                });
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                [self CompleteImageUpdate:key];
            }];
        });
    }];

注意点

由于提交的时候它的处理也是多线程的,所以用group不好使。弄了个全局变量,记录成功或者失败的个数,与提交数量相同了,认为完成。。。

你可能感兴趣的:(AFNetworking)