使用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);
}];
}
备注: 如有不足请留言指正;