添加相册或拍照更改头像

添加代理<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

首先在点击事件中添加滑出菜单

- (IBAction)editAvatarAction:(UIButton *)sender

{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"上传头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册", nil];

    [sheet showInView:self.view];
}

然后在actionSheet的代理方法中选择拍照或相册

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{
    switch (buttonIndex)
    {
        case 0://拍照
        {
            BOOL isCameraSupport = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
            if(isCameraSupport){
                UIImagePickerController *imagepicker = [[UIImagePickerController alloc]init];
                imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                imagepicker.allowsEditing = YES;
                imagepicker.delegate = self;
                [self presentViewController:imagepicker animated:YES completion:nil];
            }
        }
            break;
        case 1://相册
        {
            BOOL isCameraSupport = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
            if(isCameraSupport){
                UIImagePickerController *imagepicker = [[UIImagePickerController alloc]init];
                imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                imagepicker.allowsEditing = YES;
                imagepicker.delegate = self;
                [self presentViewController:imagepicker animated:YES completion:nil];
            }
        }
            break;            
        default:
            break;
    }
}

//最后在UIImagePickerControllerDelegate代理方法(确认选取)
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *image = info[UIImagePickerControllerEditedImage];//获取选取的图片
}

 

posted @ 2016-03-30 18:01  小屁孩~  阅读(444)  评论(0编辑  收藏  举报