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 - 调用系统相册及拍照功能实现方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];//获取到所选择的照片
TtitleImageCell *cell = [self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.img.image = img;
UIImage *compressImg = [self imageWithImageSimple:img scaledToSize:CGSizeMake(60, 60)];//对选取的图片进行大小上的压缩
[self transportImgToServerWithImg:compressImg]; //将裁剪后的图片上传至服务器
[self dismissViewControllerAnimated:YES completion:nil];
}
//用户取消选取时调用,可以用来做一些事情
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
//压缩图片方法
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
4.上传图片
//上传图片至服务器后台
-
(void)transportImgToServerWithImg:(UIImage *)img{
NSData *imageData;
NSString *mimetype;
//判断下图片是什么格式
if (UIImagePNGRepresentation(img) != nil) {
mimetype = @“image/png”;
imageData = UIImagePNGRepresentation(img);
}else{
mimetype = @“image/jpeg”;
imageData = UIImageJPEGRepresentation(img, 1.0);
}// 调用服务,上传图片数据
}
5.在info.plist文件中添加权限
Privacy - Photo Library Usage Description
App需要您的同意,才能访问相册,用于上传头像等功能
Privacy - Camera Usage Description
App需要您的同意,才能访问相机,用于上传头像等功能
Privacy - Media Libaray Usage Description
App需要您的同意,才能使用媒体资源库