美文网首页
UIImagePickerController

UIImagePickerController

作者: Silence_xl | 来源:发表于2019-05-08 17:30 被阅读0次

    UIImagePickerController简述:

    UIImagePickerController 类是获取选择图片和视频的用户接口,我们可以用UIImagePickerController选择我们所需要的图片和视频。

    注:UIImagePickerController不能够任意定制,也不可以继承生成子类。

    一、UIImagePickerController的属性:

    1、sourceType
    指定使用模式(照相机/相册/照片库)
    enum {
    UIImagePickerControllerSourceTypePhotoLibrary,
    UIImagePickerControllerSourceTypeCamera,
    UIImagePickerControllerSourceTypeSavedPhotosAlbum
    };
    
    2、BOOL showsCameraControls
    设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏。
    
    3、BOOL allowsEditing
    设置当拍照完或在相册选完照片后,是否跳到编辑模式进行图片剪裁
    showsCameraControls=Yes时才有效果。
    
    4、cameraDevice
    判断设备是否支持前置摄像头/后置摄像头
    enum {
         UIImagePickerControllerCameraDeviceRear,
         UIImagePickerControllerCameraDeviceFront
         };
    
    5、cameraFlashMode
    设置闪光灯模式
     enum {
         UIImagePickerControllerCameraFlashModeOff  = -1,
         UIImagePickerControllerCameraFlashModeAuto = 0,
         UIImagePickerControllerCameraFlashModeOn   = 1
         };
    
    6、mediaTypes
    设置相机支持的类型,拍照和录像
    + (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType
    一共有三个可选的代理方法UIImagePickerControllerDelegate 
    – imagePickerController:didFinishPickingMediaWithInfo:  
    – imagePickerControllerDidCancel:  
    – imagePickerController:didFinishPickingImage:editingInfo: 
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    info中包括选取的照片,视频的主要信息
    NSString *const UIImagePickerControllerMediaType;         选取的类型 public.image  public.movie
    NSString *const UIImagePickerControllerOriginalImage;    修改前的UIImage object.
    NSString *const UIImagePickerControllerEditedImage;      修改后的UIImage object.
    NSString *const UIImagePickerControllerCropRect;           原始图片的尺寸NSValue object containing a CGRect data type
    NSString *const UIImagePickerControllerMediaURL;          视频在文件系统中 的 NSURL地址
    
    7、cameraViewTransform
    设置拍摄时屏幕的view的transform属性,可以实现旋转,缩放功能
    enum{
    CGAffineTransformMakeRotation;
    CGAffineTransformMakeScale;
    }
    

    二、UIImagePickerController回调方法:

    1、成功获得相片或视频后的回调

    - (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];
    }
    

    2、取消照相机的回调

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
         //模态方式退出UIImagePickerController
         [imagePicker dismissModalViewControllerAnimated:YES];
     }
    

    3、保存照片成功后的回调

    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
       contextInfo:(void *)contextInfo{
         
     }
    

    相关文章

      网友评论

          本文标题:UIImagePickerController

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