#pragma mark 上传图片
+ (void)uploadImageWithPath:(NSString *)path photos:(NSArray *)photos params:(NSDictionary *)params success:(HttpUploadSuccessBlock)success failure:(HttpUploadFailureBlock)failure
{
[SVProgressHUD showProgress:-1 status:@"正在上传,请稍等."];
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPSessionManager *manager = [HttpTool sharedHttpSession];
[manager POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
for (int i = 0; i < photos.count; i ++) {
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
formatter.dateFormat=@"yyyyMMddHHmmss";
NSString *str=[formatter stringFromDate:[NSDate date]];
NSString *fileName=[NSString stringWithFormat:@"%@.jpg",str];
UIImage *image = photos[i];
NSData *imageData = UIImageJPEGRepresentation(image, 0.28);
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"upload%d",i+1] fileName:fileName mimeType:@"image/jpeg"];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
MyLog(@"uploadProgress is %lld,总字节 is %lld",uploadProgress.completedUnitCount,uploadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *resultCode = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"result_code"]];
NSString *resultInfo = [responseObject objectForKey:@"result_info"];
MyLog(@"resultInfo is %@",resultInfo);
if ([resultCode isEqualToString:@"1"]) {
[SVProgressHUD showSuccessWithStatus:@"上传成功"];
if (success == nil) return ;
success(responseObject);
}else {
[SVProgressHUD showErrorWithStatus:resultInfo];
if (failure == nil) return ;
failure();
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"上传失败"];
if (failure == nil) return ;
failure();
}];
}
利用AFN上传图片(多张)
最新推荐文章于 2024-03-09 10:00:04 发布