實(shí)用小技巧(五):通過相冊(cè)或者相機(jī)更改圖標(biāo)

版本記錄

版本號(hào) 時(shí)間
V1.0 2017.06.21

前言

很多app都有建立小組或者社區(qū)的功能,或者給某人添加幾個(gè)描述標(biāo)簽等等,這些功能都需要?jiǎng)討B(tài)的添加標(biāo)簽視圖,這一篇就講述一下添加方法。感興趣的可以看看我寫的其他小技巧
1. 實(shí)用小技巧(一):UIScrollView中上下左右滾動(dòng)方向的判斷

2. 實(shí)用小技巧(二):屏幕橫豎屏的判斷和相關(guān)邏輯
3.實(shí)用小技巧(三):點(diǎn)擊手勢(shì)屏蔽子視圖的響應(yīng)
4.實(shí)用小技巧(四):動(dòng)態(tài)的增刪標(biāo)簽視圖

需求介紹

我們做app的時(shí)候很多時(shí)候都需要上傳圖片,最常見的就是上傳用戶的頭像,這里就包括多種方式,其中最常見的就是從本地相冊(cè)或者相機(jī)拍攝上傳,這一篇我就說一下這里的實(shí)現(xiàn)。

實(shí)現(xiàn)過程

我們都知道ios9以后,蘋果出于安全性的考慮,在打開本地相冊(cè)或者相機(jī)的時(shí)候需要權(quán)限的確定,需要配置plist文件,增加兩個(gè)鍵值對(duì),具體如下圖所示。

更改權(quán)限

下面我們就直接看代碼吧。

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:@"從相冊(cè)中選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self choosePictureWithType:@"1"];
    }];
    
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"照相機(jī)" 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

實(shí)現(xiàn)結(jié)果

下面我們就看一下實(shí)現(xiàn)結(jié)果,主要如下圖所示:

原始界面
選擇相冊(cè)
相冊(cè)權(quán)限確認(rèn)
選擇圖片
圖片展示

下面我們看一下從相機(jī)選擇照片的實(shí)現(xiàn)效果。

相機(jī)權(quán)限確認(rèn)
選擇圖片
圖片展示

可見實(shí)現(xiàn)了想要的效果。

后記

這個(gè)還是很簡(jiǎn)單的,主要就是sourceType的差別,我們選擇不同的枚舉值,就可以選擇是相冊(cè)還是相機(jī)了,謝謝大家,未完,待續(xù)~~~

秋意濃
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 當(dāng)今社會(huì),“畫家”滿街走。有人學(xué)畫三天就自稱畫家了。所有學(xué)畫的人都希望有一個(gè)速成班,畢業(yè)之后就成了“著名畫家 吳冠...
    Flying_Dragon閱讀 2,054評(píng)論 5 27
  • 天下第一好事 哇哈哈~恭喜你榮獲此笑書,獲取此書一定是你今生最大的幸運(yùn)~ 獲取此書你會(huì):長(zhǎng)生不老,不死,永遠(yuǎn)青春美...
    檸檬霜閱讀 384評(píng)論 6 5
  • 小女孩撕心裂肺的哭著叫媽媽,卻只能靠著自己早上的記憶找回家的路,但孩子很小,也只記得家離集市很遠(yuǎn),早上走了好久才到...
    卡爾蔚然閱讀 556評(píng)論 0 0

友情鏈接更多精彩內(nèi)容