多图片上传服务器

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

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 218.241.181.202 wxhl60 123456 192.168.10.253 wxhl66 wxhl6...
    CYC666阅读 5,331评论 0 6
  • 技术无极限,从菜鸟开始,从源码开始。 由于公司目前项目还是用OC写的项目,没有升级swift 所以暂时SDWebI...
    充满活力的早晨阅读 14,338评论 0 2
  • 原文 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新。 1.UITableView的Group...
    无沣阅读 4,158评论 0 2
  • 小小感言本来是想写博客来着,朋友说简书的排版更好些,于是就来试试。总想着把自己开发中遇到的技术点、问题还有大大小小...
    呆呆滴木木菇凉阅读 4,587评论 1 6
  • 周六上午犯懒,10点多才起来,一股罪恶感涌上心头。 技术更新换代太快,作为程序员心理总是缺乏安全感。 为了安心...
    兴趣使然的开发阅读 1,580评论 0 0

友情链接更多精彩内容