ios afn上传图片 iOS使用AFN进行单图和多图上传的实例代码
Spykerking 人气:0想了解iOS使用AFN进行单图和多图上传的实例代码的相关内容吗,Spykerking在本文为您仔细讲解ios afn上传图片的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:ios,afn上传图片,ios,afn上传多张图片,ios,afn多图片上传,下面大家一起来学习吧。
图片上传时必要将图片进行压缩,不然会上传失败
1.单张图上传
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) { //使用日期生成图片名称 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]]; [formData appendPartWithFileData:imageData name:@"uploadFile" fileName:fileName mimeType:@"image/png"]; } success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { //上传图片成功执行回调 completion(responseObject,nil); } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) { //上传图片失败执行回调 completion(nil,error); }];
2.多图上传
多图上传和单图上传区别在于文件名称
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) { NSInteger imgCount = 0; for (NSData *imageData in imageDatas) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS"; NSString *fileName = [NSString stringWithFormat:@"%@%@.png",[formatter stringFromDate:[NSDate date]],@(imgCount)]; [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"uploadFile%@",@(imgCount)] fileName:fileName mimeType:@"image/png"]; imgCount++; } } success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { completion(responseObject,nil); } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) { completion(nil,error); }];
加载全部内容