008-修改用户头像demo

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容