实用小技巧(五):通过相册或者相机更改图标

版本记录

版本号 时间
V1.0 2017.06.21

前言

很多app都有建立小组或者社区的功能,或者给某人添加几个描述标签等等,这些功能都需要动态的添加标签视图,这一篇就讲述一下添加方法。感兴趣的可以看看我写的其他小技巧
1. 实用小技巧(一):UIScrollView中上下左右滚动方向的判断

2. 实用小技巧(二):屏幕横竖屏的判断和相关逻辑
3.实用小技巧(三):点击手势屏蔽子视图的响应
4.实用小技巧(四):动态的增删标签视图

需求介绍

我们做app的时候很多时候都需要上传图片,最常见的就是上传用户的头像,这里就包括多种方式,其中最常见的就是从本地相册或者相机拍摄上传,这一篇我就说一下这里的实现。

实现过程

我们都知道ios9以后,苹果出于安全性的考虑,在打开本地相册或者相机的时候需要权限的确定,需要配置plist文件,增加两个键值对,具体如下图所示。

更改权限

下面我们就直接看代码吧。

1.JJAvatarVC.m

#import "JJAvatarVC.h"
#import "Masonry.h"

@interface JJAvatarVC () <UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *nickNameLabel;
@property (nonatomic, strong) UIImage *pickerImage;

@end

@implementation JJAvatarVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"头像更换";
    
    [self setupUI];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    //头像
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.height.width.equalTo(@120);
    }];
    
    //昵称
    [self.nickNameLabel sizeToFit];
    [self.nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.avatarImageView);
        make.top.equalTo(self.avatarImageView.mas_bottom).offset(15.0);
    }];
}

- (void)dealloc
{
    NSLog(@"%s",__FUNCTION__);
}

#pragma mark - Object Private Function

- (void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    //头像
    UIImageView *avatarImageView = [[UIImageView alloc] init];
    avatarImageView.image = [UIImage imageNamed:@"zhanweitu"];
    avatarImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureDidTapped)];
    [avatarImageView addGestureRecognizer:tapGesture];
    [self.view addSubview:avatarImageView];
    self.avatarImageView = avatarImageView;
    
    //昵称
    UILabel *nickNameLabel = [[UILabel alloc] init];
    nickNameLabel.text = @"刀客传奇";
    nickNameLabel.textColor  = [UIColor blueColor];
    nickNameLabel.font = [UIFont boldSystemFontOfSize:17.0];
    [self.view addSubview:nickNameLabel];
    self.nickNameLabel = nickNameLabel;
}

- (void)choosePictureWithType:(NSString *)type
{
    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
    if ([type isEqualToString:@"1"]) {
        pickVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else {
        pickVC.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    pickVC.allowsEditing = YES;
    pickVC.delegate = self;
    [self presentViewController:pickVC animated:YES completion:nil];

}

#pragma mark - Action && NOtification

- (void)tapGestureDidTapped
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self choosePictureWithType:@"1"];
    }];
    
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self choosePictureWithType:@"2"];
    }];
    
    [alertVC addAction:albumAction];
    [alertVC addAction:cameraAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    self.pickerImage = info[UIImagePickerControllerOriginalImage];
    self.avatarImageView.image = self.pickerImage;
}

@end

实现结果

下面我们就看一下实现结果,主要如下图所示:

原始界面
选择相册
相册权限确认
选择图片
图片展示

下面我们看一下从相机选择照片的实现效果。

相机权限确认
选择图片
图片展示

可见实现了想要的效果。

后记

这个还是很简单的,主要就是sourceType的差别,我们选择不同的枚举值,就可以选择是相册还是相机了,谢谢大家,未完,待续~~~

秋意浓
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 当今社会,“画家”满街走。有人学画三天就自称画家了。所有学画的人都希望有一个速成班,毕业之后就成了“著名画家 吴冠...
    Flying_Dragon阅读 1,843评论 5 27
  • 天下第一好事 哇哈哈~恭喜你荣获此笑书,获取此书一定是你今生最大的幸运~ 获取此书你会:长生不老,不死,永远青春美...
    柠檬霜阅读 363评论 6 5
  • 小女孩撕心裂肺的哭着叫妈妈,却只能靠着自己早上的记忆找回家的路,但孩子很小,也只记得家离集市很远,早上走了好久才到...
    卡尔蔚然阅读 444评论 0 0