版本记录
版本号 | 时间 |
---|---|
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
的差别,我们选择不同的枚举值,就可以选择是相册还是相机了,谢谢大家,未完,待续~~~