UIImagePickerController无死角解析

github: https://github.com/wangjinshan/IJSPhotoDemo

UIImagePickerController无死角解析

需要的头文件

#import 
#import 
-(void) selectImage
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        NSLog(@"支持相机");
    }
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        NSLog(@"支持图库");
    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        NSLog(@"支持相片库");
    }
   
    if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
    {
        NSLog(@"后置摄像头能用");
    }
    if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront])
    {
        NSLog(@"前置摄像头能用");
    }
    // 指定摄像头的闪光灯是否可用
    if ([UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear])
    {
        NSLog(@"后置支持闪光灯flash and torch");
    }
    if ([UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceFront])
    {
        NSLog(@"前置支持闪光灯flash and torch");
    }
    
    //指定的源设备上可用的媒体类型,一般就是图片和视频/判断是否支持某种多媒体类型:拍照,视频
    /*
     "public.image",
     "public.movie"
     */
    NSLog(@"--1--%@",[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary]);
    NSLog(@"--2---%@",[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]);
    NSLog(@"--3--%@",[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]);
 
    /*
     获得指定摄像头上的可用捕获模式,捕获模式是枚举类型:
     UIImagePickerControllerCameraCaptureModePhoto:拍照模式
     UIImagePickerControllerCameraCaptureModeVideo:视频录制模式
     */
    NSLog(@"--4---%@",[UIImagePickerController availableCaptureModesForCameraDevice:UIImagePickerControllerCameraDeviceRear]);
    NSLog(@"--5---%@",[UIImagePickerController availableCaptureModesForCameraDevice:UIImagePickerControllerCameraDeviceFront]);
    
    // 判断当前的sourceType是否可用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
      
        UIImagePickerController * imagePickerVC = [[UIImagePickerController alloc] init];  // 实例化UIImagePickerController控制器
        /*
         UIImagePickerControllerSourceTypePhotoLibrary, //所有的相册
         UIImagePickerControllerSourceTypeCamera,       // 相机
         UIImagePickerControllerSourceTypeSavedPhotosAlbum  // 按照时间排好的图库
         */
        imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;      // 设置资源来源(相册、相机、图库之一)
      
       // 设置所支持的类型,设置只能拍照,或则只能录像,或者两者都可以
       // 媒体类型,默认情况下此数组包含kUTTypeImage,所以拍照时可以不用设置;但是当要录像的时候必须设置,可以设置为kUTTypeVideo(视频,但不带声音)或者kUTTypeMovie(视频并带有声音)
        /*
         kUTTypeImage  静态图片
         kUTTypeVideo 没有声音
         kUTTypeMovie 视频有声音
         */
        NSArray * arrMediaTypes = @[( NSString *)kUTTypeImage,( NSString *)kUTTypeMovie];
        [imagePickerVC setMediaTypes:arrMediaTypes];
        
        /*
         allowsEditing            //  允许编辑 默认是NO
         */
        imagePickerVC.allowsEditing = YES;

        // 如果选择的是视屏,允许的视屏时长为20秒
        imagePickerVC.videoMaximumDuration = 3;
        imagePickerVC.videoQuality = UIImagePickerControllerQualityTypeHigh; // 允许的视屏质量(如果质量选取的质量过高,会自动降低质量)
        
        imagePickerVC.showsCameraControls = YES;  //是否显示摄像头控制面板,默认为YES
        // 摄像头上覆盖的视图,可用通过这个视频来自定义拍照或录像界面
//        imagePickerVC.cameraOverlayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
//        imagePickerVC.cameraViewTransform = CGAffineTransformMake(0, 0, 1, 2, 3, 4);  //旋转角度
        
        /*
         UIImagePickerControllerCameraCaptureModePhoto:拍照模式
         UIImagePickerControllerCameraCaptureModeVideo:视频录制模式
         */
        //摄像头捕获模式,捕获模式是枚举类型
        imagePickerVC.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
        
        /*
         UIImagePickerControllerCameraDeviceRear:后置置摄像头
         UIImagePickerControllerCameraDeviceFront:前置摄像头
         */
        //摄像头设备,cameraDevice是枚举类型:
        imagePickerVC.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        
        /*
         UIImagePickerControllerCameraFlashModeOff:关闭闪光灯
         UIImagePickerControllerCameraFlashModeAuto:闪光灯自动
         UIImagePickerControllerCameraFlashModeOn:打开闪光灯
         */
        //闪光灯模式,枚举类型:
        imagePickerVC.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        
        /*
         对象方法
         */
//        [imagePickerVC takePicture];            //编程方式拍照
        [imagePickerVC startVideoCapture]; //   编程方式录制视频
//        [imagePickerVC stopVideoCapture];  // 编程方式停止录制视频
        
        
        // 设置代理,遵守UINavigationControllerDelegate, UIImagePickerControllerDelegate 协议
        imagePickerVC.delegate = self;
        // 是否允许编辑(YES:图片选择完成进入编辑模式)
        imagePickerVC.allowsEditing = YES;
        // model出控制器
        [self presentViewController:imagePickerVC animated:YES completion:nil];
        self.picker = imagePickerVC;
    }
}


#pragma mark UIIamgePicker 代理方法
// 媒体拾取完成
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary *)editingInfo
{
    NSLog(@"--editingInfo---%@",editingInfo);
}
// 选择结束
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"---info----%@",info);
    [self.picker dismissViewControllerAnimated:YES completion:nil];
}

// 当用户取消选取时调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
}
#pragma mark c 函数 方法
-(void) saveImageToMyAlbum
{
    void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);    //保存照片到相簿
    BOOL UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(NSString *videoPath);    //能否将视频保存到相簿
    void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, id completionTarget, SEL completionSelector, void *contextInfo);
}


//#import 
//kUTTypeImage 包含:
/*
 NSString *const  UIImagePickerControllerMediaType ; 指定用户选择的媒体类型(文章最后进行扩展)
 NSString *const  UIImagePickerControllerOriginalImage ; 原始图片
 NSString *const  UIImagePickerControllerEditedImage ; 修改后的图片
 NSString *const  UIImagePickerControllerCropRect ;    裁剪尺寸
 NSString *const  UIImagePickerControllerMediaURL ;媒体的URL
 NSString *const  UIImagePickerControllerReferenceURL ;原件的URL
 NSString *const  UIImagePickerControllerMediaMetadata; 当来数据来源是照相机的时候这个值才有效
 */
/*
 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;
 */

/*
 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;
 */

你可能感兴趣的:(UIImagePickerController无死角解析)