zoukankan      html  css  js  c++  java
  • UIImagePickerController(相册的使用)

    此工程使用Storyboard来实现的:

    #import "ViewController.h" 

    @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

    @property (strong, nonatomic) IBOutlet UIButton *buttoon;

    @property (strong, nonatomic) IBOutlet UIImageView *imageView;

    @end 

    @implementation ViewController

    - (IBAction)btAction:(UIButton *)sender {

        //创建提示框 控制器

        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示框" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

       //创建相机的点击事件按钮 

        UIAlertAction *alertA = [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 *controller = [UIAlertController alertControllerWithTitle:@"您的设备没有摄像头" message:nil preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];

                [controller addAction:action];

                [self presentViewController:controller animated:YES completion:nil];

            }

        }];    

        [alertC addAction:alertA];//添加到提示框

       //创建相册的点击事件按钮,

        UIAlertAction *action = [ UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

            UIImagePickerController *picker = [[UIImagePickerController alloc] init];

            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//指定数据来源是相册

            picker.delegate = self;

            picker.allowsEditing = YES;        

            [self presentViewController:picker animated:YES completion:nil];

        }];    

        [alertC addAction:action];    

        [self presentViewController:alertC animated:YES completion:nil];

    //选取图片之后执行的方法

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

        NSLog(@"%@",info);//是个字典 

       //通过字典的key值来找到图片  

        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];/选取的是原始图片。还有其他的样式;如编辑的图片:UIImagePickerControllerEditedImage

       //并且赋值给声明好的imageView

        self.imageView.image = image;

      //最后模态返回 最初的 控制器

        [picker dismissViewControllerAnimated:YES completion:nil];

    }

    注意:使用时要接受两个协议:<UIImagePickerControllerDelegate,UINavigationControllerDelegate>,创建UIAlertController和UIAlertAction的初始化方法名的区别,其他就是其样式的选取的区别,根据自己的需求来选择样式。弹出提示框使用的是,模态的方法弹出,返回时同样使用的是模态,方法名要熟记。

  • 相关阅读:
    Django——文件上传
    Django——视图基础
    Django——模板基础
    Django——路由基础
    Django——用户认证系统
    Django——form表单
    Django——模型基础(多表)
    Django——模型基础(单表)
    Django博客项目
    黏包
  • 原文地址:https://www.cnblogs.com/jiurong001/p/5197817.html
Copyright © 2011-2022 走看看