008-修改用户头像demo
2017-03-07 本文已影响6人
ArrQing
http://blog.csdn.net/sinat_27310637/article/details/50557073
需要注意的 几个点 :
- 属性 可以是在 C中 也可以在 V 中 V中的话 就 在 中 再给个 属性 就好了
- info 配置 问题 :
NSContactsUsageDescription -> 通讯录
NSMicrophoneUsageDescription -> 麦克风
NSPhotoLibraryUsageDescription -> 相册
NSCameraUsageDescription -> 相机
NSLocationAlwaysUsageDescription -> 地理位置
NSLocationWhenInUseUsageDescription -> 地理位置
- 需要实现的 四个 协议 :
<UIActionSheetDelegate,UIGestureRecognizerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
接下来 我就直接 上代码了
//
// ViewController.m
// 001
//
// Created by ArrQ on 2017/2/27.
// Copyright © 2017年 ArrQ. All rights reserved.
//
#import "ViewController.h"
#import <AFNetworking.h>
@interface ViewController ()<UIActionSheetDelegate,UIGestureRecognizerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImageView *iamgeView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.imgView];
}
- (UIImageView *)imgView{
if (!_imgView) {
_imgView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 100, 250, 50)];
// 可以加个 判断 登陆之后 才 可以点击
_imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGetsture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(alterHeadPortrait:)];
[_imgView addGestureRecognizer:tapGetsture];
_imgView.backgroundColor = [UIColor redColor];
}
return _imgView;
}
#pragma mark --- 头像实现方法
-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
PickerImage.allowsEditing = YES;
PickerImage.delegate = self;
[self presentViewController:PickerImage animated:YES completion:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
PickerImage.allowsEditing = YES;
PickerImage.delegate = self;
[self presentViewController:PickerImage animated:YES completion:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
// 头像选取之后的 替换保存
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
_imgView.image = newPhoto;
[self dismissViewControllerAnimated:YES completion:nil];
}
@end