iOS开发中 ----更换头像图片--代码块

这里写图片描述
这里写图片描述这里写图片描述这里写图片描述

()<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];

}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值