标签:
最近服务器做了一个可以支持多张图片上传的接口,所以我对应的也找了AFN多图片上传的方法
这里我主要是针对UImage数组的传入,我觉得大部分我都是能获得UImage来上传的,然而在网上找到的方法都是要获得图片的地址,所以就自己整了这个
#pragma mark - 上传图片 - (void)uploadImages:(NSArray <UIImage *>*)images { WEAKSELF; AFHTTPSessionManager *mar = [AFHTTPSessionManager manager]; NSMutableDictionary *params = [NSMutableDictionary dictionary]; //添加服务器需要你传的参数 params NSString *url = nil;
//拼接地址 url = [NSString stringWithFormat:@"%@%@", BASE_URL, URL_SHOP_IMAGE_UPLOAD]; [mar POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { //拼接图片 [images enumerateObjectsUsingBlock:^(UIImage * _Nonnull image, NSUInteger idx, BOOL * _Nonnull stop) { NSData *imageData = UIImagePNGRepresentation(image); if (imageData.length >= 1024 * 1024) {
//因为我们服务器有限制1M以内 所以我超过1M的进行压缩了 imageData = [image resetSizeOfImageData:image maxSize:1000]; [formData appendPartWithFileData:imageData name:@"kinta" fileName:@"kinta.jpg" mimeType:@"image/jpg"]; }else{ [formData appendPartWithFileData:imageData name:@"kinta" fileName:@"kinta.png" mimeType:@"image/png"]; } }]; } progress:^(NSProgress * _Nonnull uploadProgress) { // 回到主队列刷新UI,用户自定义的进度条 dispatch_async(dispatch_get_main_queue(), ^{ [SVProgressHUD showProgress:1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount]; }); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"上传成功 %@", responseObject); id datalist = responseObject[@"datalist"]; NSLog(@"%@",datalist); } else { } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"上传失败 %@", error); [SVProgressHUD dismiss]; }]; }
标签:
原文地址:http://www.cnblogs.com/yulongjiayuan/p/5857871.html