版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.06.21 |
前言
很多app都有建立小組或者社區(qū)的功能多望,或者給某人添加幾個(gè)描述標(biāo)簽等等善延,這些功能都需要?jiǎng)討B(tài)的添加標(biāo)簽視圖贷痪,這一篇就講述一下添加方法。感興趣的可以看看我寫(xiě)的其他小技巧
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í)候都需要上傳圖片帖鸦,最常見(jiàn)的就是上傳用戶(hù)的頭像芝薇,這里就包括多種方式,其中最常見(jiàn)的就是從本地相冊(cè)或者相機(jī)拍攝上傳作儿,這一篇我就說(shuō)一下這里的實(shí)現(xiàn)洛二。
實(shí)現(xiàn)過(guò)程
我們都知道ios9以后,蘋(píng)果出于安全性的考慮攻锰,在打開(kāi)本地相冊(cè)或者相機(jī)的時(shí)候需要權(quán)限的確定灭红,需要配置plist文件,增加兩個(gè)鍵值對(duì)口注,具體如下圖所示变擒。
下面我們就直接看代碼吧。
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);
}];
//昵稱(chēng)
[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;
//昵稱(chēng)
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é)果寝志,主要如下圖所示:
下面我們看一下從相機(jī)選擇照片的實(shí)現(xiàn)效果娇斑。
可見(jiàn)實(shí)現(xiàn)了想要的效果。
后記
這個(gè)還是很簡(jiǎn)單的材部,主要就是
sourceType
的差別毫缆,我們選擇不同的枚舉值,就可以選擇是相冊(cè)還是相機(jī)了乐导,謝謝大家苦丁,未完,待續(xù)~~~