iOS开发笔记:实现修改头像

刚转行iOS的搬砖工人,在此记录下这条路上的点点滴滴,共勉

在之前的笔记中,讲了如何实现圆形头像,这里接着上一次的笔记,讲一下怎么修改头像(通过图库和拍照方式)。

重点

手势(UITapGestureRecognizer)、
提示框(UIAlertController)、
相册拍照(UIImagePickerController)

流程

一般在APP中,修改头像是最基本的功能之一了。一般是两种方式的修改:从相册选择图片或者拍照。那么这里就来讲一下如何具体实现这个功能。

Step1:点击头像 ->手势(UITapGestureRecognizer)

iOS开发笔记:实现修改头像_第1张图片
QQ的更换头像操作

首先,点击头像。因为头像是直接放在ImageView中的,默认情况下当我们点击头像的时候,头像是不会有任何反应的。因此,我们需要给头像的ImageView添加一个点击事件,方法如下:

    /**
     *  添加手势:也就是当用户点击头像了之后,对这个操作进行反应
     */
        //允许用户交互
    _myHeadPortrait.userInteractionEnabled = YES;
        //初始化一个手势
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self
                                                                action:@selector(alterHeadPortrait:)];
        //给ImageView添加手势
    [_myHeadPortrait addGestureRecognizer:singleTap];
    }

Step2:弹出选择提示->提示框(UIAlertController)

通过添加UITapGestureRecognizer(手势),系统就知道了我点击了头像,接着,就可以添加具体的方法来进行操作了。在上一步,我为这个手势的action,selector(选择)了一个方法来执行,即alterHeadPortrait:(注意有冒号的),也就是当我们点击了头像之后,会执行alterHeadPortrait:这个方法:

//  方法:alterHeadPortrait
-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{
    /**
     *  弹出提示框
     */
        //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        //按钮:从相册选择,类型:UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:nil]];
        //按钮:拍照,类型:UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:nil]];
        //按钮:取消,类型:UIAlertActionStyleCancel
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

通过UIAlertController(提示框)这个类,我们创建好了一个提示框,如下:

iOS开发笔记:实现修改头像_第2张图片
点击头像,弹出提示框

现在,当我们点击取消(或者点击按钮以外的区域)提示框就会被自动取消掉,并将提示框收起来。

Step3:从相册选择或者拍照选择头像->UIImagePickerController

好了,绕了这么久,终于开始进入主题了,即选择图片或者拍照了。那么现在该肿么办呢?好像毫无头绪的样子。。。
这里就需要通过UIImagePickerController,通过它,我们就可以让我们的APP轻松的实现访问相册或者拍照:
操作UIImagePickerController,需要实现两个协议:


进行相册图片选择或者相机拍照的实现代码如下:

            //初始化UIImagePickerController
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
            //获取方式1:通过相册(呈现全部相册),UIImagePickerControllerSourceTypePhotoLibrary
            //获取方式2,通过相机,UIImagePickerControllerSourceTypeCamera
            //获取方方式3,通过相册(呈现全部图片),UIImagePickerControllerSourceTypeSavedPhotosAlbum
        PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//方式1
            //允许编辑,即放大裁剪
        PickerImage.allowsEditing = YES;
            //自代理
        PickerImage.delegate = self;
            //页面跳转
        [self presentViewController:PickerImage animated:YES completion:nil];

运行效果如图:


iOS开发笔记:实现修改头像_第3张图片
选择图片,自由裁剪

Step4:替换头像->大功告成!

现在,我们已经能够打开相册,或者拍照(拍照功能模拟机无法拍照,会报错,只有用真机测试)。
可是问题来了,现在选择了新图片,确定之后,头像还是原来的头像,并没有更新。这是因为我们这里还没有对图片选择完全之后的代理方法进行实现:

//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //定义一个newPhoto,用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    //把newPhono设置成头像
    _myHeadPortrait.image = newPhoto;
    //关闭当前界面,即回到主界面去
    [self dismissViewControllerAnimated:YES completion:nil];
}

大功告成:


iOS开发笔记:实现修改头像_第4张图片
修改头像成功

完全代码如下:


#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *myHeadPortrait;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //  调用setHeadPortrait方法
    [self setHeadPortrait];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//  方法:设置头像样式
-(void)setHeadPortrait{
    //  把头像设置成圆形
    self.myHeadPortrait.layer.cornerRadius=self.myHeadPortrait.frame.size.width/2;
    self.myHeadPortrait.layer.masksToBounds=YES;
    //  给头像加一个圆形边框
    self.myHeadPortrait.layer.borderWidth = 1.5f;
    self.myHeadPortrait.layer.borderColor = [UIColor whiteColor].CGColor;
    
    /**
     *  添加手势:也就是当用户点击头像了之后,对这个操作进行反应
     */
        //允许用户交互
    _myHeadPortrait.userInteractionEnabled = YES;
    
        //初始化一个手势
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self
                                                                action:@selector(alterHeadPortrait:)];
    
        //给imageView添加手势
    [_myHeadPortrait addGestureRecognizer:singleTap];
    }

//  方法:alterHeadPortrait
-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{
    /**
     *  弹出提示框
     */
            //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            //按钮:从相册选择,类型:UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //初始化UIImagePickerController
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
            //获取方式1:通过相册(呈现全部相册),UIImagePickerControllerSourceTypePhotoLibrary
            //获取方式2,通过相机,UIImagePickerControllerSourceTypeCamera
            //获取方法3,通过相册(呈现全部图片),UIImagePickerControllerSourceTypeSavedPhotosAlbum
        PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //允许编辑,即放大裁剪
        PickerImage.allowsEditing = YES;
            //自代理
        PickerImage.delegate = self;
            //页面跳转
        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
        //按钮:拍照,类型:UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        /**
         其实和从相册选择一样,只是获取方式不同,前面是通过相册,而现在,我们要通过相机的方式
         */
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
            //获取方式:通过相机
        PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
        PickerImage.allowsEditing = YES;
        PickerImage.delegate = self;
        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
        //按钮:取消,类型:UIAlertActionStyleCancel
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}
//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //定义一个newPhoto,用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    _myHeadPortrait.image = newPhoto;
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

你可能感兴趣的:(iOS开发笔记:实现修改头像)