最基本的添加头像

@interface PTABabyEditBaseVC ()<UIImagePickerControllerDelegate,UIActionSheetDelegate>

@property (nonatomic) UIImagePickerController *imagePickerController;

@end

@implementation PTABabyEditBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
   .......
    
    _headBtn = [[UIButton alloc]initWithFrame:CGRectMake((WIDTH(self.view)-85)/2.0, (HEIGHT(headView)-85)/2.0, 85, 85)];
    [_headBtn.layer setMasksToBounds:YES];
    [_headBtn.layer setCornerRadius:WIDTH(_headBtn)/2.0];
    [_headBtn setBackgroundColor:[UIColor grayColor]];
    [_headBtn addTarget:self action:@selector(toShowCamera) forControlEvents:UIControlEventTouchUpInside];
    [_headBtn setImage:[UIImage imageNamed:@"loginIcon.png"] forState:UIControlStateNormal];
    [headView addSubview:_headBtn];
    
     ........
}

/**
 *  底部弹框
 */
- (void)toShowCamera{
    
    UIActionSheet * actionsheet = [[UIActionSheet alloc] initWithTitle:nil
                                                              delegate:self
                                                     cancelButtonTitle:@"取消"
                                                destructiveButtonTitle:nil
                                                     otherButtonTitles: @"从相册选取",@"拍照", nil];
    [actionsheet showInView:self.view];
}

#pragma mark --
#pragma mark -- UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(buttonIndex==0){
        [self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }else if(buttonIndex==1){
        UIImagePickerController *   imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];
        
    }
    
}

- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
    imagePickerController.sourceType = sourceType;
    imagePickerController.delegate = self;
    
    self.imagePickerController = imagePickerController;
    [self presentViewController:self.imagePickerController animated:YES completion:nil];
}



#pragma mark --
#pragma mark -- UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) {
        UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        [_headBtn setImage:image forState:UIControlStateNormal];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
原文地址:https://www.cnblogs.com/allanliu/p/4482202.html