• UIImagePickerController


    iOS 获取图片有三种方法:

    1. 直接调用摄像头拍照

    2. 从相册中选择

    3. 从图库中选择

    UIImagePickerController 是系统提供的用来获取图片和视频的接口;

    用UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:

    1. 初始化UIImagePickerController 类;

    2. 设置UIImagePickerController 实例的数据来源类型(下面解释);

    3. 设置设置代理;

    4. 如果需要做图片修改的话设置allowsEditing =yes。

    数据来源类型一共有三种:

    1
    2
    3
    4
    5
    enum {
       UIImagePickerControllerSourceTypePhotoLibrary ,//来自图库
       UIImagePickerControllerSourceTypeCamera ,//来自相机
       UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册
    };

    在用这些来源的时候最好检测以下设备是否支持;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            NSLog(@"支持相机");
        }
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            NSLog(@"支持图库");
        }
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
        {
            NSLog(@"支持相片库");
        }

    调用摄像头来获取资源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    - (void)viewDidLoad {
        [super viewDidLoad];
        picker = [[UIImagePickerController alloc]init];
        picker.view.backgroundColor = [UIColor orangeColor];
        UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
        picker.sourceType = sourcheType;
        picker.delegate = self;
        picker.allowsEditing = YES;
    }

    上面只是实例了UIImagePickerController及其属性 在需要获取图片的时候需要弹出窗口调用

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

    我们还需要代理来获取我们选中的图片

    1
    UIImagePickerControllerDelegate

    代理中一共三个方法 其中一个3.0 已经废弃了,只剩下两个我们需要用的

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

    当用户选取完成后调用;

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

    当用户取消选取时调用;

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

    选取的信息都在info中,info 是一个字典。

    字典中的键:

    1
    2
    3
    4
    5
    6
    7
    NSString *const  UIImagePickerControllerMediaType ;指定用户选择的媒体类型(文章最后进行扩展)
    NSString *const  UIImagePickerControllerOriginalImage ;原始图片
    NSString *const  UIImagePickerControllerEditedImage ;修改后的图片
    NSString *const  UIImagePickerControllerCropRect ;裁剪尺寸
    NSString *const  UIImagePickerControllerMediaURL ;媒体的URL
    NSString *const  UIImagePickerControllerReferenceURL ;原件的URL
    NSString *const  UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效

    UIImagePickerController 的更多参数参考这里

    代理中的功能参考这里

    UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie

    KUTTypeImage 包含:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    const CFStringRef  kUTTypeImage ;抽象的图片类型
    const CFStringRef  kUTTypeJPEG ;
    const CFStringRef  kUTTypeJPEG2000 ;
    const CFStringRef  kUTTypeTIFF ;
    const CFStringRef  kUTTypePICT ;
    const CFStringRef  kUTTypeGIF ;
    const CFStringRef  kUTTypePNG ;
    const CFStringRef  kUTTypeQuickTimeImage ;
    const CFStringRef  kUTTypeAppleICNS 
    const CFStringRef kUTTypeBMP;
    const CFStringRef  kUTTypeICO;

    KUTTypeMovie 包含:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    const CFStringRef  kUTTypeAudiovisualContent ;抽象的声音视频
    const CFStringRef  kUTTypeMovie ;抽象的媒体格式(声音和视频)
    const CFStringRef  kUTTypeVideo ;只有视频没有声音
    const CFStringRef  kUTTypeAudio ;只有声音没有视频
    const CFStringRef  kUTTypeQuickTimeMovie ;
    const CFStringRef  kUTTypeMPEG ;
    const CFStringRef  kUTTypeMPEG4 ;
    const CFStringRef  kUTTypeMP3 ;
    const CFStringRef  kUTTypeMPEG4Audio ;
    const CFStringRef  kUTTypeAppleProtectedMPEG4Audio;
     
     

    转载自:http://blog.csdn.net/kingsley_cxz/article/details/9157093

    1.UIImagePickerController的静态方法:

        imagepicker = [[UIImagePickerController alloc]init];
        //UIImagePickerController静态方法判断设备是否支持照相机/图片库/相册功能
        /*
         typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
         UIImagePickerControllerSourceTypePhotoLibrary,
         UIImagePickerControllerSourceTypeCamera,
         UIImagePickerControllerSourceTypeSavedPhotosAlbum
         };
         */
        BOOL isCameraSupport = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
        NSLog(@"support camera:%d",isCameraSupport);
        
        //UIImagePickerController静态方法判断设备是否支持前置摄像头/后置摄像头
        /*
         typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
         UIImagePickerControllerCameraDeviceRear,
         UIImagePickerControllerCameraDeviceFront
         };
         */
        BOOL isRearSupport = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
        NSLog(@"rear support:%d",isRearSupport);
        
        //UIImagePickerController静态方法判断设备是否支持前置摄像头闪光灯/后置摄像头闪光灯
        /*
         typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
         UIImagePickerControllerCameraDeviceRear,
         UIImagePickerControllerCameraDeviceFront
         };
         */
        BOOL isFlushSupport = [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear];
        NSLog(@"rear flash support:%d",isFlushSupport);
        
        //UIImagePickerController静态方法返回前置摄像头/后置摄像头支持的拍摄类型
        /*
         typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
         UIImagePickerControllerCameraDeviceRear,
         UIImagePickerControllerCameraDeviceFront
         };
         */
        //返回类型有照片和视频
        /*
         enum {
         UIImagePickerControllerCameraCaptureModePhoto,
         UIImagePickerControllerCameraCaptureModeVideo
         };
         typedef NSUInteger UIImagePickerControllerCameraCaptureMode;
         */
        NSArray* captureModes = [UIImagePickerController availableCaptureModesForCameraDevice:UIImagePickerControllerCameraDeviceRear];
        for (NSNumber* mode in captureModes) {
            NSLog(@"capture modes:%d",[mode integerValue]);
        }
        
        //UIImagePickerController静态方法返回照相机/相册/照片库所支持的媒体类型
        /*
         typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
         UIImagePickerControllerSourceTypePhotoLibrary,
         UIImagePickerControllerSourceTypeCamera,
         UIImagePickerControllerSourceTypeSavedPhotosAlbum
         };
         */
        //返回类型有kUTTypeMovie,kUTTypeImage,其他类型均在<MobileCoreServices/MobileCoreServices.h>下
        NSArray* mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        for (NSString* type in mediaTypes) {
            NSLog(@"media types:%@",type);
        }

    2.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];
    }

    使用 imagepicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI*45/180);旋转45度的效果:

    使用imagepicker.cameraViewTransform = CGAffineTransformMakeScale(1.5, 1.5);全屏的效果,同时imagepicker.showsCameraControls  =NO;隐藏工具栏:

    使用imagepicker.allowsEditing = YES;出现的图片编辑效果,只有imagepicker.showsCameraControls  = YES;才有效果:

    使用 imagepicker.mediaTypes = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeMovie];支持拍照和视频的前后对比效果:

    3.UIImagePickerController回调详解:

    //成功获得相片还是视频后的回调
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
        //通过UIImagePickerControllerMediaType判断返回的是照片还是视频
        NSString* type = [info objectForKey:UIImagePickerControllerMediaType];
        //如果返回的type等于kUTTypeImage,代表返回的是照片,并且需要判断当前相机使用的sourcetype是拍照还是相册
        if ([type isEqualToString:(NSString*)kUTTypeImage]&&picker.sourceType==UIImagePickerControllerSourceTypeCamera) {
            //获取照片的原图
            UIImage* original = [info objectForKey:UIImagePickerControllerOriginalImage];
            //获取图片裁剪的图
            UIImage* edit = [info objectForKey:UIImagePickerControllerEditedImage];
            //获取图片裁剪后,剩下的图
            UIImage* crop = [info objectForKey:UIImagePickerControllerCropRect];
            //获取图片的url
            NSURL* url = [info objectForKey:UIImagePickerControllerMediaURL];
            //获取图片的metadata数据信息
            NSDictionary* metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
            //如果是拍照的照片,则需要手动保存到本地,系统不会自动保存拍照成功后的照片
            UIImageWriteToSavedPhotosAlbum(edit, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }else{
        
        }
        //模态方式退出uiimagepickercontroller
        [imagepicker dismissModalViewControllerAnimated:YES];
    }
    //取消照相机的回调
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        //模态方式退出uiimagepickercontroller
        [imagepicker dismissModalViewControllerAnimated:YES];
    }
    //保存照片成功后的回调
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
      contextInfo:(void *)contextInfo{
        
        NSLog(@"saved..");
    }

    4.自定义相机拍照画面:

       //设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏
        imagepicker.showsCameraControls  = NO;

        UIToolbar* tool = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-55, self.view.frame.size.width, 75)];
        tool.barStyle = UIBarStyleBlackTranslucent;
        UIBarButtonItem* cancel = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelCamera)];
        UIBarButtonItem* add = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePhoto)];
        [tool setItems:[NSArray arrayWithObjects:cancel,add, nil]];
       //把自定义的view设置到imagepickercontroller的overlay属性中

       imagepicker.cameraOverlayView = tool;

    -(void)cancelCamera{
        [imagepicker dismissModalViewControllerAnimated:YES];
    }
    -(void)savePhoto{
        //拍照,会自动回调- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info,对于自定义照相机界面,拍照后回调可以不退出实现连续拍照效果
        [imagepicker takePicture];
    }

    5.UIImagePickerController是继承UINavigationController,所以可以push和pop一些viewcontroller进行导航效果。例如,自定义照相机画面的时候可以在拍摄完后push一个viewcontroller用于对照片进行编辑

  • 相关阅读:
    wide&deep模型演化
    ES6常用知识点小结
    json 的循环输出
    javascript闭包问题
    controller层中,参数的获取方式以及作用域的问题
    SSM整合开发
    Myself
    Hyperledger Fabric相关文件解析
    Fabric1.4源码解析:链码实例化过程
    Fabric1.4源码解析:Peer节点启动过程
  • 原文地址:https://www.cnblogs.com/worldtraveler/p/4582978.html
Copyright © 2020-2023  润新知