一般开发上传用户头像使用AF框架就可以实现代码如下,有需要的可以参考下:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"]; manager.requestSerializer.timeoutInterval = 8.f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
[KVNProgress showWithStatus:@"正在上传..." onView:self.view]; // 随意用一个提示的弹框都可以
[manager POST:@“你的API接口” parameters:@“你的其余参数” constructingBodyWithBlock:^(id_Nonnull formData) {
[formData appendPartWithFileData:@“你的图片转为二进制”name:@"file" fileName:@“可以随意写一个文件名” mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
[KVNProgress showSuccessWithStatus:@"更换成功" onView:self.view];
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
[KVNProgress showErrorWithStatus:@"网络异常请稍后再试" onView:self.view];
} ];
很简单,要是上传多张可以写一个循环上传就OK ,fileName 就用循环的次数来命名避免重复! 如有问题或者更好的建议欢迎留言,谢谢!