zoukankan      html  css  js  c++  java
  • iOS设置用户头像(从相册,图库或者拍照获取)

    ①初始化UIImagePickerController

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

     ②遵守协议

    @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

    //设置代理  
    _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<NSString *,id> *)editingInfo
    {
    
    _selectedRightImage.image=image;
    [self dismissViewControllerAnimated:YES completion:nil];
    
    }

    最终效果图:

     

     

    一个人,一片天,一条路,一瞬间!
  • 相关阅读:
    Shiro入门学习之shi.ini实现授权(三)
    Shiro入门学习之shi.ini实现认证及源码分析(二)
    猜字母游戏(Java)
    二维数组的语法
    鸡兔同笼问题(Java)
    成绩统计程序(Java)
    18位身份证验证(Java)加入身份证输入验证是否满足18位代码(修订稿)
    18位身份证验证(Java)
    键盘输入字符插入定义数组中并按顺序排列
    一个随机验证码且不重复的小程序以及求随机输入一组数组中的最大值(Java)
  • 原文地址:https://www.cnblogs.com/zcl410/p/4962562.html
Copyright © 2011-2022 走看看