zoukankan      html  css  js  c++  java
  • UIImagePickerController和UIAlertController结合使用

    在处理个人资料 - 头像的时候,通常有两个选项,一个是调用系统相机,一个是调用系统相册。这里要使用的就是UIImagePickerController方法。

    在头像位置的imageView添加一个手势,或者添加一个透明的按钮,用来实现click方法,直接上代码:

    - (IBAction)click:(id)sender{
    
        //创建提醒视图
    
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    
             //判断设备是否存在摄像头,有就调用系统相机,没有,就提醒用户
    
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    
                //创建相机
    
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
                //文件由来
    
                picker.sourceType = UIImagePickerControllerSourceTypeCamera; //指定数据来源来自于相机
    
                picker.delegate  = self;// 指定代理
    
                picker.allowsEditing = YES; //允许编辑
    
                
    
                //模态弹出
    
                [self presentViewController:picker animated:YES completion:nil];
    
            }else{
    
                //没有摄像头,提醒用户 您的设备没有摄像头
    
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您的设备没有摄像头" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
                UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
    
                [alertController addAction:alertAction1];
    
                [self presentViewController:alertController animated:YES completion:nil];
    
            }
    
        }];
    
        [alertController addAction:alertAction];
    
        UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
            UIImagePickerController *pickerC = [[UIImagePickerController alloc] init];
    
            pickerC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//指定数据来源为相册
    
            pickerC.delegate = self;  //指定代理
    
            pickerC.allowsEditing = YES;  // 允许编辑
    
            [self presentViewController:pickerC animated:YES completion:nil];
    
        }];
    
        [alertController addAction:alertAction2];
    
        [self presentViewController:alertController animated:YES completion:nil];
    
    }
    
     
    
    //选取图片之后执行的方法
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
        NSLog(@"%@",info);
    
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
        self.photoImage.image = image;
    
        [picker dismissViewControllerAnimated:YES completion:nil];
    
    }
  • 相关阅读:
    linux驱动开发学习一:创建一个字符设备
    如何高效的对有序数组去重
    找到缺失的第一个正整数
    .NET不可变集合已经正式发布
    中国人唯一不认可的成功——就是家庭的和睦,人生的平淡【转】
    自己动手搭建 MongoDB 环境,并建立一个 .NET HelloWorld 程序测试
    ASP.NET MVC 中如何用自定义 Handler 来处理来自 AJAX 请求的 HttpRequestValidationException 错误
    自己动手搭建 Redis 环境,并建立一个 .NET HelloWorld 程序测试
    ServiceStack 介绍
    一步一步实战扩展 ASP.NET Route,实现小写 URL、个性化 URL
  • 原文地址:https://www.cnblogs.com/Walking-Jin/p/5468379.html
Copyright © 2011-2022 走看看