_urls_Str = [NSMutableString string];
NSMutableArray * op_Arr = [NSMutableArray array];
if (items.count > 0)
{
__block int i = 0;
for (UIImage * image in items)
{
NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:@"http://jiaoxue.bjbkws.com/upLoad.php" parameters:nil constructingBodyWithBlock:^(id? _Nonnull formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation(image) name:@"file" fileName:[NSString stringWithFormat:@"hehe%d",i++] mimeType:@"image/png"];
} error:nil];
//為創(chuàng)建的請(qǐng)求設(shè)置執(zhí)行
AFHTTPRequestOperation * op = [[AFHTTPRequestOperation alloc]initWithRequest:request];
//設(shè)置執(zhí)行過(guò)程中的上傳進(jìn)度
[op setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"已經(jīng)上傳:%lf%%",((double)totalBytesWritten/totalBytesExpectedToWrite)*100);
}];
//當(dāng)當(dāng)前這張圖片上傳完成后調(diào)用
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id? _Nonnull responseObject) {
//請(qǐng)求成功后解析返回的數(shù)據(jù)
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
if (dic)
{
NSString * isS = dic[@"success"];
//當(dāng)圖片上傳成功
if ([isS isEqualToString:@"0"])
{
//拿到url
NSString * url = dic[@"url"];
[_urls_Str appendFormat:@"%@|",url];
}else
{
NSLog(@"upload Error");
}
}else
{
NSLog(@"解析失??!");
}
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
NSLog(@"op Error = %@",error.localizedDescription);
}];
[op_Arr addObject:op];
}
NSArray * ops = [AFURLConnectionOperation batchOfRequestOperations:op_Arr progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"total %ld fnished %ld",totalNumberOfOperations,numberOfFinishedOperations);
} completionBlock:^(NSArray * _Nonnull operations) {
//截取最后拼接的|
self.image_url=[self.urls_Str substringToIndex:self.urls_Str.length-1];
self.image_url//內(nèi)部存放圖片地址
}];
[_queue addOperations:ops waitUntilFinished:NO];
}