美文网首页
iOS 视频开发自己写 UIImagePickerControl

iOS 视频开发自己写 UIImagePickerControl

作者: superKelly | 来源:发表于2019-04-09 17:58 被阅读0次

    UIImagePickerController是苹果给的轻便型多媒体控件,完成选择图片视频拍照拍视频的功能。


    步骤

    步骤1 定义

    self.pickerController = [[UIImagePickerController alloc] init];
    

    步骤2 设备校验

    - (BOOL) isFrontCameraAvailable{
        return [UIImagePickerController
                isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
    }
    - (BOOL) isRearCameraAvailable{
        return [UIImagePickerController
                isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    }
    - (BOOL) isPhotoLibrarySourceTypeAvailable{
        return [UIImagePickerController
                isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary];
    }
    - (BOOL) isCameraSourceTypeAvailable{
        return [UIImagePickerController
                isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera];
    }
    - (BOOL) isSavedPhotosAlbumSourceTypeAvailable{
        return [UIImagePickerController
                isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    }
    - (BOOL) isFrontCameraFlashAvailable{
        return [UIImagePickerController
                isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceFront];
    }
    - (BOOL) isRearCameraFlashAvailable{
        return [UIImagePickerController
                isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear];
    }
    

    step3 配置

        self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.pickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        self.pickerController.mediaTypes = @[(NSString*)kUTTypeMovie];
        self.pickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
        self.pickerController.cameraFlashMode = UIApplicationShortcutIconTypeCaptureVideo;
        self.pickerController.allowsEditing = YES;
        self.pickerController.delegate = self;
    

    step4 实现代理

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<UIImagePickerControllerInfoKey,id> *)info
    {
        NSLog([info description]);
        NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { //如果是拍照
            UIImage * image;
            //如果允许编辑则获得编辑后的照片,否则获取原始照片
            if (_pickerController.allowsEditing) {
                image = [info objectForKey:UIImagePickerControllerEditedImage];//获取编辑后的照片
            } else {
                image = [info objectForKey:UIImagePickerControllerOriginalImage];//获取原始照片
            }
            
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//保存到相簿
            
        }
        else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){ //如果是录制视频
            NSURL * url = [info objectForKey:UIImagePickerControllerMediaURL];
            NSString * urlStr = [url path];
            if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
                //保存视频到相簿,注意也可以使用ALAssetsLibrary来保存
                UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
            }
        }
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    

    API说明

    枚举类型

    typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
        UIImagePickerControllerSourceTypePhotoLibrary,  // 图片库
        UIImagePickerControllerSourceTypeCamera,   // 摄像头
        UIImagePickerControllerSourceTypeSavedPhotosAlbum //相机照片
    } __TVOS_PROHIBITED;
    
    typedef NS_ENUM(NSInteger, UIImagePickerControllerQualityType) {
        UIImagePickerControllerQualityTypeHigh = 0,       // highest quality
        UIImagePickerControllerQualityTypeMedium = 1,     // medium quality, suitable for transmission via Wi-Fi 
        UIImagePickerControllerQualityTypeLow = 2,         // lowest quality, suitable for tranmission via cellular network
        UIImagePickerControllerQualityType640x480 NS_ENUM_AVAILABLE_IOS(4_0) = 3,    // VGA quality
        UIImagePickerControllerQualityTypeIFrame1280x720 NS_ENUM_AVAILABLE_IOS(5_0) = 4,
        UIImagePickerControllerQualityTypeIFrame960x540 NS_ENUM_AVAILABLE_IOS(5_0) = 5,
    } __TVOS_PROHIBITED;
    
    typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraCaptureMode) {
        UIImagePickerControllerCameraCaptureModePhoto,
        UIImagePickerControllerCameraCaptureModeVideo
    } __TVOS_PROHIBITED;
    
    typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
        UIImagePickerControllerCameraDeviceRear,
        UIImagePickerControllerCameraDeviceFront
    } __TVOS_PROHIBITED;
    
    闪光灯
    typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
        UIImagePickerControllerCameraFlashModeOff  = -1,
        UIImagePickerControllerCameraFlashModeAuto = 0,
        UIImagePickerControllerCameraFlashModeOn   = 1
    } __TVOS_PROHIBITED;
    
    苹果在 iOS 11 的发布会上,推出了两种新的媒体格式 HEIF HEVC
    都是为了保证画质的情况下,大大减少视频、照片的大小。
    安卓能否打开?
    typedef NS_ENUM(NSInteger, UIImagePickerControllerImageURLExportPreset) {
        UIImagePickerControllerImageURLExportPresetCompatible = 0,
        UIImagePickerControllerImageURLExportPresetCurrent
    } NS_AVAILABLE_IOS(11_0) __TVOS_PROHIBITED;
    
    

    info dictionary

    typedef NSString * UIImagePickerControllerInfoKey NS_TYPED_ENUM;
    
    // info dictionary keys
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerMediaType __TVOS_PROHIBITED;      // an NSString (UTI, i.e. kUTTypeImage)
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerOriginalImage __TVOS_PROHIBITED;  // a UIImage
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerEditedImage __TVOS_PROHIBITED;    // a UIImage
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerCropRect __TVOS_PROHIBITED;       // an NSValue (CGRect)
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerMediaURL __TVOS_PROHIBITED;       // an NSURL
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerReferenceURL        NS_DEPRECATED_IOS(4_1, 11_0, "Replace with public API: UIImagePickerControllerPHAsset") __TVOS_PROHIBITED;  // an NSURL that references an asset in the AssetsLibrary framework
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerMediaMetadata       NS_AVAILABLE_IOS(4_1) __TVOS_PROHIBITED;  // an NSDictionary containing metadata from a captured photo
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerLivePhoto NS_AVAILABLE_IOS(9_1) __TVOS_PROHIBITED;  // a PHLivePhoto
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerPHAsset NS_AVAILABLE_IOS(11_0) __TVOS_PROHIBITED;  // a PHAsset
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerImageURL NS_AVAILABLE_IOS(11_0) __TVOS_PROHIBITED;  // an NSURL
    
    

    function

    
    + (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;               
    //判断设备是否支持指定sourceType
    // returns YES if source is available (i.e. camera present)
    
    + (nullable NSArray<NSString *> *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType; 
    //返回设备支持的sourceType列表
    // returns array of available media types (i.e. kUTTypeImage)
    
    + (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice                   NS_AVAILABLE_IOS(4_0);
    //是否支持camera
    // returns YES if camera device is available 
    
    + (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice           NS_AVAILABLE_IOS(4_0); 
    //是否支持闪光灯
    // returns YES if camera device supports flash and torch.
    
    + (nullable NSArray<NSNumber *> *)availableCaptureModesForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice NS_AVAILABLE_IOS(4_0);
    //返回支持的设备类型列表
     // returns array of NSNumbers (UIImagePickerControllerCameraCaptureMode)
    
    

    变量

    UIImagePickerControllerSourceType     sourceType;                                                        // default value is UIImagePickerControllerSourceTypePhotoLibrary.
    NSArray<NSString *>                   *mediaTypes;
        // default value is an array containing kUTTypeImage.
    BOOL                                  allowsEditing NS_AVAILABLE_IOS(3_1);     // replacement for -allowsImageEditing; default value is NO.
    UIImagePickerControllerImageURLExportPreset imageExportPreset NS_AVAILABLE_IOS(11_0);   // default value is UIImagePickerControllerImageExportPresetCompatible.
    
    // video properties apply only if mediaTypes includes kUTTypeMovie
    NSTimeInterval                        videoMaximumDuration NS_AVAILABLE_IOS(3_1); // default value is 10 minutes.
    UIImagePickerControllerQualityType    videoQuality NS_AVAILABLE_IOS(3_1);         // default value is UIImagePickerControllerQualityTypeMedium. If the cameraDevice does not support the videoQuality, it will use the default value.
    NSString                              *videoExportPreset NS_AVAILABLE_IOS(11_0);  // videoExportPreset can be used to specify the transcoding quality for videos (via a AVAssetExportPreset* string). If the value is nil (the default) then the transcodeQuality is determined by videoQuality instead. Not valid if the source type is UIImagePickerControllerSourceTypeCamera
    
    
    // camera additions available only if sourceType is UIImagePickerControllerSourceTypeCamera.
    BOOL                                  showsCameraControls NS_AVAILABLE_IOS(3_1);   // set to NO to hide all standard camera UI. default is YES
     __kindof UIView                *cameraOverlayView  NS_AVAILABLE_IOS(3_1);   // set a view to overlay the preview view.
    CGAffineTransform                     cameraViewTransform NS_AVAILABLE_IOS(3_1);   // set the transform of the preview view.
    
    UIImagePickerControllerCameraCaptureMode cameraCaptureMode NS_AVAILABLE_IOS(4_0); // default is UIImagePickerControllerCameraCaptureModePhoto
     UIImagePickerControllerCameraDevice      cameraDevice      NS_AVAILABLE_IOS(4_0); // default is UIImagePickerControllerCameraDeviceRear
    UIImagePickerControllerCameraFlashMode   cameraFlashMode   NS_AVAILABLE_IOS(4_0); // default is UIImagePickerControllerCameraFlashModeAuto. 
    // cameraFlashMode controls the still-image flash when cameraCaptureMode is Photo. cameraFlashMode controls the video torch when cameraCaptureMode is Video.
    
    

    function

    - (void)takePicture NS_AVAILABLE_IOS(3_1);                                                   
    // programatically initiates still image capture. ignored if image capture is in-flight.
    // clients can initiate additional captures after receiving -imagePickerController:didFinishPickingMediaWithInfo: delegate callback
    
    - (BOOL)startVideoCapture NS_AVAILABLE_IOS(4_0);
    - (void)stopVideoCapture  NS_AVAILABLE_IOS(4_0);
    

    delegate

    // The picker does not dismiss itself; the client dismisses it in these callbacks.用户需要自己去关闭
    // The delegate will receive one or the other, but not both, depending whether the user
    //操作完毕
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info;
    //操作取消
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
    
    

    UIKIT_EXTERN函数,经过处理的extern

    
    
    // Adds a photo to the saved photos album.  The optional completionSelector should have the form:
    //  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
    UIKIT_EXTERN void UIImageWriteToSavedPhotosAlbum(UIImage *image, __nullable id completionTarget, __nullable SEL completionSelector, void * __nullable contextInfo) __TVOS_PROHIBITED;
    
    // Is a specific video eligible to be saved to the saved photos album? 
    UIKIT_EXTERN BOOL UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(NSString *videoPath) NS_AVAILABLE_IOS(3_1) __TVOS_PROHIBITED;
    
    // Adds a video to the saved photos album. The optional completionSelector should have the form:
    //  - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
    UIKIT_EXTERN void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, __nullable id completionTarget, __nullable SEL completionSelector, void * __nullable contextInfo) NS_AVAILABLE_IOS(3_1) __TVOS_PROHIBITED;
    
    

    **NS_AVAILABLE_IOS(5_0) **
    这个方法可以在iOS5.0及以后的版本中使用,如果在比5.0更老的版本中调用这个方法,就会引起崩溃。
    NS_DEPRECATED_IOS(2_0, 6_0)
    这个宏中有两个版本号。前面一个表明了这个方法被引入时的iOS版本,后面一个表明它被废弃时的iOS版本。被废弃并不是指这个方法就不存在了,只是意味着我们应当开始考虑将相关代码迁移到新的API上去了。

    相关文章

      网友评论

          本文标题:iOS 视频开发自己写 UIImagePickerControl

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