注:示例代码AFNetworking版本为3.0 粗略封装,大牛勿喷
.h里面代码
/**
* 上传带图片的内容,允许多张图片上传(URL)POST
*
* @param url 网络请求地址
* @param images 要上传的图片数组(注意数组内容需是图片)
* @param parameter 图片数组对应的参数
* @param parameters 其他参数字典
* @param succeedBlock 成功的回调
* @param failedBlock 失败的回调
* @param uploadProgressBlock 上传进度的回调
*/
+ (void)startMultiPartUploadTaskWithURL:(NSString *)url
imagesArray:(NSArray *)images
parameterOfimages:(NSString *)parameter
parametersDict:(NSDictionary *)parameters
succeedBlock:(void(^)(id task, id responseObject))succeedBlock
failedBlock:(void(^)(id task, NSError *error))failedBlock
uploadProgressBlock:(void(^)(float uploadPercent,long long totalBytesWritten,long long totalBytesExpectedToWrite))uploadProgressBlock;
.m里面代码,注意适配https -> 适配https看这里
+ (void)startMultiPartUploadTaskWithURL:(NSString *)url
imagesArray:(NSArray *)images
parameterOfimages:(NSString *)parameter
parametersDict:(NSDictionary *)parameters
succeedBlock:(void (^)(id, id))succeedBlock
failedBlock:(void (^)(id, NSError *))failedBlock
uploadProgressBlock:(void (^)(float, long long, long long))uploadProgressBlock{
if (images.count == 0) {
NSLog(@"上传内容没有包含图片");
return;
}
for (int i = 0; i < images.count; i++) {
if (![images[i] isKindOfClass:[UIImage class]]) {
NSLog(@"images中第%d个元素不是UIImage对象",i+1);
return;
}
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager setSecurityPolicy:[Factory customSecurityPolicy]];//此处为适配https设置
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData)
{
int i = 0;
//根据当前系统时间生成图片名称
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy年MM月dd日"];
NSString *dateString = [formatter stringFromDate:date];
for (UIImage *image in images) {
NSString *fileName = [NSString stringWithFormat:@"%@%d.png",dateString,i];
NSData *imageData;
imageData = [Factory imageData:image];//对图片进行压缩处理,也是封装的,这部分代码放在下面
[formData appendPartWithFileData:imageData name:parameter fileName:fileName mimeType:@"image/jpg/png/jpeg"];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
CGFloat percent = 1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;
uploadProgressBlock(percent,uploadProgress.completedUnitCount,uploadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[MBProgressHUD hideHUD];
succeedBlock(task,responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[MBProgressHUD hideHUD];
[MBProgressHUD showError:@"上传失败"];
failedBlock(task,error);
NSLog(@"请求失败:%@",error);
}];
}
[Factory imageData:image]这部分代码的封装
// 图片压缩
+ (NSData *)imageData:(UIImage *)myimage
{
NSData *data = UIImageJPEGRepresentation(myimage, 1.0);
if (data.length > 100 * 1024) {
if (data.length > 1024 * 1024) {// 1M以及以上
data = UIImageJPEGRepresentation(myimage, 0.1);
}else if (data.length > 512 * 1024) {// 0.5M-1M
data = UIImageJPEGRepresentation(myimage, 0.5);
}else if (data.length > 200 * 1024) {// 0.25M-0.5M
data = UIImageJPEGRepresentation(myimage, 0.9);
}
}
return data;
}