其他分享
首页 > 其他分享> > ios客户端学习-上传头像

ios客户端学习-上传头像

作者:互联网

1.首先添加代理,因为使用到了imagePicker
UINavigationControllerDelegate
UIImagePickerControllerDelegate

2.给头像的imageview添加手势,从底部弹出actionSheet来选择拍照或者相册选择
// 选择头像
-(void)selectImg{
//底部弹出来个actionSheet来选择拍照或者相册选择
UIAlertController *userIconActionSheet = [UIAlertController alertControllerWithTitle:@“请选择上传类型” message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//相册选择
UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@“手机相册选择” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//这里加一个判断,是否是来自图片库
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self; //协议
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}];
//系统相机拍照
UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@“相机” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@“取消” style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[userIconActionSheet addAction:albumAction];
[userIconActionSheet addAction:photoAction];
[userIconActionSheet addAction:cancelAction];
[self presentViewController:userIconActionSheet animated:YES completion:nil];
}

3.代理方法
#pragma mark - 调用系统相册及拍照功能实现方法

}

//用户取消选取时调用,可以用来做一些事情

//压缩图片方法

4.上传图片
//上传图片至服务器后台

5.在info.plist文件中添加权限
Privacy - Photo Library Usage Description
App需要您的同意,才能访问相册,用于上传头像等功能
Privacy - Camera Usage Description
App需要您的同意,才能访问相机,用于上传头像等功能
Privacy - Media Libaray Usage Description
App需要您的同意,才能使用媒体资源库

标签:UIAlertAction,img,self,ios,UIImagePickerController,imagePicker,YES,上传,客户端
来源: https://blog.csdn.net/liudan302698481/article/details/109955386