ios 刷新头像_iOS设置用户头像(从相册,图库或者拍照获取)

①初始化UIImagePickerController

self.imagePicker=[[UIImagePickerController alloc] init];

②遵守协议

@interface ViewController ()

//设置代理

_imagePicker.delegate=self;

//可编辑

_imagePicker.allowsEditing=YES;

//设置头像图片圆角

_selectedRightImage.layer.cornerRadius=100;

_selectedRightImage.layer.borderWidth=6;

_selectedRightImage.layer.masksToBounds=YES;

_selectedRightImage.layer.borderColor=[[UIColor whiteColor] CGColor];

self.selectedRightImage.userInteractionEnabled=YES;

⑤给图片添加点击事件

UITapGestureRecognizer *tapPicture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectedImageForIcon)];

[_selectedRightImage addGestureRecognizer:tapPicture];

⑥从相册,图库,相机获取图片

-(void)selectedImageForIcon

{

UIAlertController*alertController=[UIAlertController alertControllerWithTitle:@"" message:@""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 *)editingInfo

{

_selectedRightImage.image=image;

[self dismissViewControllerAnimated:YES completion:nil];

}

最终效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值