使用AFNetworing多图上传

开发中单张图片上传这里不再赘述,下面我们看一下多图上传;

1.使用base64转码之后上传

NSMutableDictionary *param = [NSMutableDictionary dictionary];

//需要上传的图片数组数组

NSMutableArray *imgsArr = [NSMutableArray array];

//self.imgs 图片数据来源

for (UIImage *img in self.imgs) {

NSData *imageData = UIImagePNGRepresentation(image);

NSString *baseImg = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

[imgsArr addObject:baseImg];

}

param[@"imgs"] = imgsArr;

//url: 服务器地址 param: 参数信息

[LQHttpTool post:url parameters:param success:^(id responseObject) {

NSLog(@"上传成功");

} faliure:^(NSError *error) {

NSLog(@"上传失败");

}];

2.使用formData表单上传

NSMutableDictionary *param = [NSMutableDictionary dictionary];         

[LQHttpTool post:uploadBlog parameters:param constructingBodyWithBlock:^(idformData) {

//利用for循环上传多张图片

int i = 1;

for (UIImage * image in self.mImgs) {

//把图片转换为二进制流

NSData *imageData = UIImagePNGRepresentation(image);

//按照表单格式把二进制文件写入formData表单

[formData appendPartWithFileData:imageData name:@"imgs" fileName:[NSString stringWithFormat:@"%d.png", i] mimeType:@"image/png"];

i++;

}

} success:^(id responseObject) {

NSLog(@"发布成功!!");

} faliure:^(NSError *error) {

NSLog(@"发布失败!!");

}];

>>LQHttpTool.h内容

+ (LQHttpTool *)sharedManager;

/** *  post请求 */

+ (void)post:(NSString *)url parameters:(id)parameters success:(void(^)(id responseObject))success faliure:(void(^)(NSError *error))failure;

/** *  文件上传 * *  @param url        接口url *  @param parameters 参数 *  @param block      图片data *  @param success    请求成功的block *  @param failure    请求失败的block */

+ (void )post:(NSString *)url parameters:(id)parameters  constructingBodyWithBlock:(void (^)(idformData))block success:(void (^)(id responseObject))success faliure:(void (^)(NSError *error))failure;

>>LQHttpTool.m内容

+ (LQHttpTool *)sharedManager {

static LQHttpTool *handle = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

handle = [LQHttpTool manager];

handle.requestSerializer.timeoutInterval = 20;

handle.requestSerializer = [AFJSONRequestSerializer serializer];

handle.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"application/json",@"text/json",@"text/javascript",@"text/html",nil];

});

//请求头

[handle.requestSerializer setValue:@"userId" forHTTPHeaderField:@"userId"];

[handle.requestSerializer setValue:@"token" forHTTPHeaderField:@"token"];

return handle;

}

+ (void)post:(NSString *)url parameters:(id)parameters success:(void(^)(id responseObject))success faliure:(void(^)(NSError *error))failure {

[[LQHttpTool sharedManager] POST:[BASE_URL stringByAppendingString:url] parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

if (success) {

success(responseObject);

}

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;

NSInteger statusCode = [response statusCode];

KLog(@"%ld",(long)statusCode);

failure(error);

}];

}


+ (void )post:(NSString *)url parameters:(id)parameters  constructingBodyWithBlock:(void (^)(idformData))block success:(void (^)(id responseObject))success faliure:(void (^)(NSError *error))failure{       

[[LQHttpTool sharedManager] POST:[BASE_URL stringByAppendingString:url] parameters:parameters constructingBodyWithBlock:^(id_Nonnull formData) {

block(formData);

} progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

if(responseObject)  {

success(responseObject);

}

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

failure(error);

}];

}

备注: 如有不足请留言指正;

推荐阅读更多精彩内容

  • //需要AFN //.h //AFNetworking + (void)post:(NSString *)url ...
    CHADHEA阅读 466评论 0 0
  • .打开下载下来的demo,运行如下(其中红色箭头所指的文件是我们拖到项目中的两个文件): 1678515-d4b8...
    Littleredguy阅读 432评论 0 0
  • 有很多人面试时候都被问到,你是怎么封装网络请求的昵?? 所以来聊聊,如何封装网络请求 //// HTTPTool....
    Evan叶少阅读 373评论 0 3
  • 网络请求的时候最常用到的就是get,post和upload了。 @interface UploadParam : ...
    xh_0129阅读 227评论 0 1
  • 拆书帮30拆便签04 片段一:选自《精进》 R:我自己工作中有一个习惯,就是拿到一个任务后,务必要先找到那个任务的...
    歌舒雪阅读 91评论 0 0