zoukankan      html  css  js  c++  java
  • ios硬件开发 照相机图像选取器(UIImagePickerController)的用法

    1.新建Empty Application,添加一个视图,设计xib如下:

     

    2.使用 UIImagePickerController ,必须指定几个非常重要的属性:指定源的类型(指定之前要判断是否可用)、指定委托、指定图片是否可以编辑,设置完之后,就可以启动了,将其“推”出来即可。

     

     - (IBAction)takeNewPhoto:(id)sender 

    {
        //创建图片选择器
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        
        //指定源类型前,检查图片源是否可用
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            //指定源的类型
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            //在选定图片之前,用户可以简单编辑要选的图片。包括上下移动改变图片的选取范围,用手捏合动作改变图片的大小等。
            imagePicker.allowsEditing = YES;
            
            //实现委托,委托必须实现UIImagePickerControllerDelegate协议,来对用户在图片选取器中的动作
            imagePicker.delegate = self;
            
            //设置完iamgePicker后,就可以启动了。用以下方法将图像选取器的视图“推”出来
            [self presentModalViewController:imagePicker animated:YES];
            
            [imagePicker release];
        }
        else
        {
            UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"相机不能用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }

     

    3. UIImagePickerControllerDelegate协议的方法来实现对用户的操作

    #pragma  mark -
    #pragma  mark UIImagePickerControllerDelegate协议的方法

    //用户点击图像选取器中的“cancel”按钮时被调用,这说明用户想要中止选取图像的操作
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
        [picker dismissModalViewControllerAnimated:YES];
    }

    //用户点击选取器中的“choose”按钮时被调用,告知委托对象,选取操作已经完成,同时将返回选取图片的实例
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
    {
        noticeLabel.hidden = YES;
        [imageView setImage:image];
        [picker dismissModalViewControllerAnimated:YES];

  • 相关阅读:
    linux相关的常用站点
    基于命令行的网络调试和测试工具
    清除DNS缓存
    数组映射
    react-native 自定义多选
    weex 长按图片保存
    MySql常用总结
    git常用命令
    react-native 自制多选功能
    react-native setState无法保持更新
  • 原文地址:https://www.cnblogs.com/hanjun/p/2782627.html
Copyright © 2011-2022 走看看