多图片上传服务器

_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];

//为创建的请求设置执行

AFHTTPRequestOperation * op = [[AFHTTPRequestOperation alloc]initWithRequest:request];

//设置执行过程中的上传进度

[op setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

NSLog(@"已经上传:%lf%%",((double)totalBytesWritten/totalBytesExpectedToWrite)*100);

}];

//当当前这张图片上传完成后调用

[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {

//请求成功后解析返回的数据

NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

if (dic)

{

NSString * isS = dic[@"success"];

//当图片上传成功

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//内部存放图片地址

}];

[_queue addOperations:ops waitUntilFinished:NO];

}

你可能感兴趣的:(多图片上传服务器)