注:示例代碼AFNetworking版本為3.0 粗略封裝,大牛勿噴
.h里面代碼
/**
* 上傳帶圖片的內(nèi)容治唤,允許多張圖片上傳(URL)POST
*
* @param url 網(wǎng)絡(luò)請(qǐng)求地址
* @param images 要上傳的圖片數(shù)組(注意數(shù)組內(nèi)容需是圖片)
* @param parameter 圖片數(shù)組對(duì)應(yīng)的參數(shù)
* @param parameters 其他參數(shù)字典
* @param succeedBlock 成功的回調(diào)
* @param failedBlock 失敗的回調(diào)
* @param uploadProgressBlock 上傳進(jìn)度的回調(diào)
*/
+ (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(@"上傳內(nèi)容沒有包含圖片");
return;
}
for (int i = 0; i < images.count; i++) {
if (![images[i] isKindOfClass:[UIImage class]]) {
NSLog(@"images中第%d個(gè)元素不是UIImage對(duì)象",i+1);
return;
}
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager setSecurityPolicy:[Factory customSecurityPolicy]];//此處為適配https設(shè)置
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;
//根據(jù)當(dāng)前系統(tǒng)時(shí)間生成圖片名稱
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];//對(duì)圖片進(jìn)行壓縮處理,也是封裝的概说,這部分代碼放在下面
[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(@"請(qǐng)求失敾斤薄:%@",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;
}