美文网首页
AF3.0以上图片批量上传

AF3.0以上图片批量上传

作者: BLUEVIPIOS_ | 来源:发表于2017-09-06 15:21 被阅读0次

实现af 3.0 以后 图片多张上传的封装  没升级3.0的af 更便捷,不知为何被舍弃掉了,只能自己搞下了。不过已封装好。

封装一下还是挺方便的。都已整理好,复制即可

1. 把图片数组转为data数据 副值与新的数组

NSMutableArray*array=[NSMutableArray array];

for (int i=0; i<_ImagePothosView.selectedPhotos.count; i++) {

UIImage*image=_ImagePothosView.selectedPhotos[i];

NSData *data=UIImageJPEGRepresentation(image, 0.3);

[array addObject:data];

}

2. 根据要求设置相应参数 

NSMutableDictionary*dictionary=[NSMutableDictionary dictionary];

dictionary[@"userId"]=UID;

3. 调取上传的a f 3.0方法

[[ApiHttpTool sharedClient]uploadMostImageWithURLString:WPuploadpicServerUrl parameters:dictionary uploadDatas:array success:^(id json){

NSLog(@"%@",json);

} failure:^(NSError *erroe) {

}];

#pragma mark -- 图片上传 --

- (void)uploadMostImageWithURLString:(NSString *)URLString

parameters:(id)parameters

uploadDatas:(NSArray *)uploadDatas

success:(void (^)(id))success

failure:(void (^)())failure{

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];

4设置 超时时间

manager.requestSerializer.timeoutInterval = 60.f;

[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];

[manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id< AFMultipartFormData >  _Nonnull formData) {

for (int i=0; I<uploadDatas.count; I++){

5根据 图片文件设置。 img/jpg格式

NSString *imageName = [NSString stringWithFormat:@"%@[%i]", @"img", i];

[formData appendPartWithFileData:uploadDatas[i] name:@"img" fileName:imageName mimeType:@"image/jpeg"];

} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

NSLog(@"%@",responseObject);

if (success) {

success(responseObject);

}

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

if (failure) {

failure(error);

}

}];

相关文章

网友评论

      本文标题:AF3.0以上图片批量上传

      本文链接:https://www.haomeiwen.com/subject/mofvjxtx.html