版本記錄
| 版本號(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ù)~~~

秋意濃