首页 > 系统 > iOS > 正文

iOS多张图片总是只能上传一张的问题

2019-11-09 18:37:51
字体:
来源:转载
供稿:网友

最近在做图片上传的功能,使用的AFNetworking提供的方法上传图片,发现多张图片时总是只有一张图片上传到后台。

有问题的上传代码为:

特别提醒:

创建AFHTTPsessionManager时,直接使用[AFHTTPSessionManager manager]会造成内存泄漏,需要实现单列方法,可参看AFNetworking的Demo

        AFHTTPSessionManager *manager = [TPHTTPTool sharedHTTPManager];        manager.responseSerializer = [AFJSONResponseSerializer serializer];        //设置响应头可以接收的content-type        NSMutableSet *tempSet = [manager.responseSerializer.acceptableContentTypes mutableCopy];        [tempSet addObjectsFromArray:@[@"text/html"]];        manager.responseSerializer.acceptableContentTypes = [tempSet copy];        [manager POST:URL_detailPatientUpdate parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {            for(NSInteger i = 0; i < images.count; i++){                //取出单张图片二进制数据                id obj = images[i];                UIImage *image = nil;                if ([obj isKindOfClass:[UIImage class]]) {                    image = (UIImage *)obj;                }else{                    image = [UIImage imageWithContentsOfFile:obj];                }                if (image) {                    NSData *imageData = [TPDataTool dataWithImage:image maxKB:500.0f];                    NSLog(@"图片大小:%ld K" , (long)imageData.length / 1024);                    // 上传filename                    NSString *fileName = [NSString stringWithFormat:@"image_%ld.jpg", (long)blockI];                    [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];                }            }        } PRogress:^(NSProgress * _Nonnull uploadProgress) {                    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responSEObject) {                    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {                    }];

找后台调试后发现,通过上面的方法上传多张图片,实际上只有一张被传到后台,可能的原因是:

上传多张图片时,由于url的参数没有变化,iOS默认是同一个请求,只发送了一次请求给后台,故后台只收到一张图片。

解决办法是将循环把整个请求方法都包起来。代码为:

// 上传多张图片    for(NSInteger i = 0; i < images.count; i++)    {        AFHTTPSessionManager *manager = [TPHTTPTool sharedHTTPManager];        manager.responseSerializer = [AFJSONResponseSerializer serializer];        //设置响应头可以接收的content-type        NSMutableSet *tempSet = [manager.responseSerializer.acceptableContentTypes mutableCopy];        [tempSet addObjectsFromArray:@[@"text/html"]];        manager.responseSerializer.acceptableContentTypes = [tempSet copy];        __block NSInteger blockI = i;        [manager POST:URL_detailPatientUpdate parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {            //取出单张图片二进制数据            id obj = images[blockI];            UIImage *image = nil;            if ([obj isKindOfClass:[UIImage class]]) {                image = (UIImage *)obj;            }else{                image = [UIImage imageWithContentsOfFile:obj];            }            if (image) {                NSData *imageData = [TPDataTool dataWithImage:image maxKB:500.0f];                NSLog(@"图片大小:%ld K" , (long)imageData.length / 1024);                // 上传filename                NSString *fileName = [NSString stringWithFormat:@"image_%ld.jpg", (long)blockI];                [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];            }        } progress:^(NSProgress * _Nonnull uploadProgress) {                    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {                    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {                   }];}


上一篇:iOS wifi认证

下一篇:iOS仿QQ分组效果

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表