最新版的AF已经废弃了很多以前的类,所以很多以前的方法都不能用了,当然最主要还是为了适应ipV6所做的更改。楼主最近正在写多张图片上传,碰到了一些问题,解决之后直接封装了一个方法,废话有点多了,上代码:
//上传多张图片
+ (void)requestWithUrl:(NSString *)url
withPostedImages:(NSArray *)imagesArray
WithSuccessBlock:(void (^)(NSArray * resultArray))successBlock
WithNeebHub:(BOOL)needHub
WithView:(UIView *)viewWithHub
WithData:(NSDictionary *)dataDic
{
if (imagesArray.count > 0) {
//创建一个临时的数组,用来存储回调回来的结果
NSMutableArray *temArray = [NSMutableArray array];
for (int i = 0; i < imagesArray.count; i++) {
UIImage *imageObj = imagesArray[i];
//截取图片
NSData *imageData = UIImageJPEGRepresentation(imageObj, 0.5);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 访问路径
[manager POST:url parameters:dataDic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
// 上传文件
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
[formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
[temArray addObject:dic];
//当所有图片上传成功后再将结果进行回调
if (temArray.count == imagesArray.count) {
successBlock(temArray);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
}
}
在将所有图片上传成功后,在进行回调,回调的是一个数组哦,这样在上传图片的界面就可以拿到所有上传图片的id了。。。。