iOS-个人整理14 - UIImagePickerContro

作者: 简单也好 | 来源:发表于2016-04-13 11:54 被阅读2868次

    一、UIImagePickerController

    这个文章大部分摘别人的,自己总结下来更方便用一点

    UIImagePickerController是系统提供的用来获取图片和视频的接口
    获取的途径为相机,图库,相册
    分为以下几个步骤:

    1. 初始化UIImagePickerController 类;
    2. 设置UIImagePickerController 实例的数据来源类型(下面解释);
    3. 设置代理;
    4. 如果需要做图片修改的话设置allowsEditing =yes。
      数据来源的类型有三种
    enum {  
       UIImagePickerControllerSourceTypePhotoLibrary ,//来自图库  
       UIImagePickerControllerSourceTypeCamera ,//来自相机  
       UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册  
    };  
    

    用之前要检测设备是否支持

    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  
       {  
           NSLog(@"支持相机");  
       }  
       if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])  
       {  
           NSLog(@"支持图库");  
       }  
       if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])  
       {  
           NSLog(@"支持相片库");  
       }  
    

    二、UIImagePickerController属性详解:

        //指定使用照相机模式,可以指定使用相册/照片库    
    imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
        //设置当拍照完或在相册选完照片后,是否跳到编辑模式进行图片剪裁。只有当showsCameraControls属性为true时才有效果    
    imagepicker.allowsEditing = YES;
    
        //设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏   
     imagepicker.showsCameraControls  = YES;
    
        //设置使用后置摄像头,可以使用前置摄像头    
    imagepicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;    
    //设置闪光灯模式    
    /*     typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {     UIImagePickerControllerCameraFlashModeOff  = -1,     UIImagePickerControllerCameraFlashModeAuto = 0,     UIImagePickerControllerCameraFlashModeOn   = 1     };     */    
    imagepicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;    
    //设置相机支持的类型,拍照和录像   
     imagepicker.mediaTypes = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeMovie];
    
        //设置拍摄时屏幕的view的transform属性,可以实现旋转,缩放功能   
    // imagepicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI*45/180);   
    // imagepicker.cameraViewTransform = CGAffineTransformMakeScale(1.5, 1.5);    
    //所有含有cameraXXX的属性都必须要sourceType是UIImagePickerControllerSourceTypeCamera时设置才有效果,否则会有异常
    
     //设置UIImagePickerController的代理   
     imagepicker.delegate = self;
    
    -(void)showCamera{    //用模态方式呈现imagepicker,这个方法之后就会跳到对应的源位置,相机或者相册图库    [self presentModalViewController:imagepicker animated:YES];}
    

    代理方法
    当用户选取完成后调用;

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;  
    

    当用户取消选取时调用;

    
    - (void)imagePickerController:(UIImagePickerController *)picker  
     didFinishPickingMediaWithInfo:(NSDictionary *)info;  
    

    最后是我自己的实现,一般也就这么用了,点击相框进入相册,选一张图点choose,出来后相框就是选择的图了

    #import "ThirdViewController.h"  
      
    //签订协议  
    @interface ThirdViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>  
      
    @property (nonatomic,retain)UIImageView *myImageView;  
      
    @end  
      
    @implementation ThirdViewController  
      
    - (void)viewDidLoad {  
        [super viewDidLoad];  
          
        //创建一块imageView  
        _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((414-300)/2, 200, 300, 300)];  
        _myImageView.backgroundColor = [UIColor whiteColor];  
        //打开交互  
        _myImageView.userInteractionEnabled = YES;  
        [self.view addSubview:_myImageView];  
          
        //添加一个手势  
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectImage:)];  
        [_myImageView addGestureRecognizer:tapGesture];  
    }  
      
    //实现手势的方法,进入相册  
    -(void)selectImage:(UIButton*)sender  
    {   //创建ImagePickController  
        UIImagePickerController *myPicker = [[UIImagePickerController alloc]init];  
          
        //创建源类型  
        UIImagePickerControllerSourceType mySourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
          
        myPicker.sourceType = mySourceType;  
          
        //设置代理  
        myPicker.delegate = self;  
        //设置可编辑  
        myPicker.allowsEditing = YES;  
        //通过模态的方式推出系统相册  
        [self presentViewController:myPicker animated:YES completion:^{  
            NSLog(@"进入相册");  
        }];  
       
    }  
    
    #pragma mark -- 实现imagePicker的代理方法  
      
     -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info  
    {  
        //取得所选取的图片,原大小,可编辑等,info是选取的图片的信息字典  
        UIImage *selectImage = [info objectForKey:UIImagePickerControllerEditedImage];  
          
        //设置图片进相框  
        self.myImageView.image = selectImage;  
        [picker dismissViewControllerAnimated:YES completion:^{  
            NSLog(@"模态返回") ;  
        }];  
    }  
      
    @end  
    

    效果如下
    中间有个相框,点一下进入相册,在相册中选择一张图片,点choose,出来相框的图片就变了


    相关文章

      网友评论

      • d15eb9289fc7:请问choose怎么改成中文?去工程中的project里面设置,但是没有可选项。。求解
        简单也好:@龙战神 完美,你的名字这么中二
        d15eb9289fc7:@简单也好 已解决。。localization native deve.....后面改成China :blush:
        简单也好:@龙战神 这个我百度过,忘了

      本文标题:iOS-个人整理14 - UIImagePickerContro

      本文链接:https://www.haomeiwen.com/subject/avzilttx.html