zoukankan      html  css  js  c++  java
  • 添加相册或拍照更改头像

    添加代理<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];//获取选取的图片
    }
    
  • 相关阅读:
    2020年封装APP之详解
    Linux 强制卸载硬盘 (Device is busy)
    pacman 非交互状态使用
    Snakemake 修改默认工作目录
    LaTeX 表格排版中遇到 Misplaced oalign
    重启崩溃的 KDE
    python robot.libraries.BuiltIn import BuiltIn库
    logging 常用配置
    paramiko 获取远程服务器文件
    物理时间使用Python脚本转格林卫时间
  • 原文地址:https://www.cnblogs.com/wlsxmhz/p/5338204.html
Copyright © 2011-2022 走看看