()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>//遵守协议
{
UIImagePickerController * _imagePicker;
UIImageView * se;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_imagePicker=[[UIImagePickerController alloc] init];
_imagePicker.delegate=self;
//可编辑图片
_imagePicker.allowsEditing=YES;
// 设置图片
se = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
se.image = [UIImage imageNamed:@"2.jpg"];
[self.view addSubview:se];
// 切圆角
se.layer.cornerRadius=100;
se.layer.borderWidth=6;
se.layer.masksToBounds=YES;
se.layer.borderColor=[[UIColor whiteColor] CGColor];
se.userInteractionEnabled=YES;
//这个方法添加手势 点击
UITapGestureRecognizer *tapPicture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectedImageForIcon)];
[se addGestureRecognizer:tapPicture];
}
//UIimageview 点击方法
-(void)selectedImageForIcon
{
// 弹出框内容及 更换头像方法
// 访问相机的情况下 必须用真机测试
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionCamera=[UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
_imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:_imagePicker animated:YES completion:nil];
}];
UIAlertAction *actionPhotoLIbrary=[UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
_imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_imagePicker animated:YES completion:nil];
}];
UIAlertAction *actionPhotoAlbum=[UIAlertAction actionWithTitle:@"打开图库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
_imagePicker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:_imagePicker animated:YES completion:nil];
}];
UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:actionCamera];
[alertController addAction:actionPhotoAlbum];
[alertController addAction:actionPhotoLIbrary];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
//更换头像方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo
{
se.image=image;
[self dismissViewControllerAnimated:YES completion:nil];
}