美文网首页
iOS UIImagePickerController 录像和拍

iOS UIImagePickerController 录像和拍

作者: 桀骜不驯的搬砖者 | 来源:发表于2018-05-23 21:52 被阅读0次

    最近在项目中,使用到相机,然后自己还不是很懂,因为自己接触的项目不是很常用到,但是现在接触的项目要做拍照上传头像功能,然后自己就在各个博客,搬了一些砖,但是还是不太理解,然后有时间自己就多多造一些轮子。 然后先去官网看看,有没有demo,找到了很久以前的一个(传送门)。然后自己就Coding了一遍。

    UIImagePickerController 的理解

    1. 它是系统统一提供UI的,好像并不能修改它的UI,但是能控制它的显示View,通过cameraOverlayView属性
    2.它的使用场景:拍照(不支持Live Photo)、录像、存取图片和video
    3.简单使用:
     //1. 创建相机对象
        imagePicker = [[UIImagePickerController alloc] init];
        //2. 设置相机属性
        //2.1 来源: 有相机、 图库、相簿
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        //2.2 媒体类型:⚠️ 最好先设置 mediaTypes, 再设置 cameraCaptureMode,
        // kUTTypeImage (拍照,默认)
        // kUTTypeVideo(视频,但不带声音)
        // kUTTypeMovie(视频并带有声音)
        // kUTTypeLivePhoto (取得 Live Photo) > iOS 9
        imagePicker.mediaTypes  = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//[NSArray arrayWithObjects:(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie, nil];
        //2.3 相机类型:录像或拍照
        imagePicker.cameraCaptureMode =  UIImagePickerControllerCameraCaptureModeVideo;
        imagePicker.allowsEditing = NO;// 允许图片编辑,拍照后可以简单的编辑图片
        imagePicker.showsCameraControls = YES;
        imagePicker.cameraViewTransform = CGAffineTransformIdentity;
        //2.4 设置摄像头 :设置之前一定要先判断device 是否有后摄像头,不是每个device 都有前后摄像头的
        if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
            imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
            if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
                cameraSelectionButton.alpha = 1.0;
                showCameraSelection = YES;
            }
        } else{
            imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        }
        //2.5 设置闪光灯 :设置之前一定要先判断device 是否支持
        if ([UIImagePickerController isFlashAvailableForCameraDevice:imagePicker.cameraDevice]) {
            imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
            flashModeButton.alpha = 1.0;
            showFlashMode = YES;
        }
        imagePicker.videoMaximumDuration = 10;//10s 它默认最大录像时间是 10 minute = 600s
        //iOS 11 之后使用 videoExportPreset
        if (@available(iOS 11.0, *)) {
            imagePicker.videoExportPreset = AVAssetExportPreset1280x720;
        } else {
            // iOS 11 之前
             imagePicker.videoQuality = UIImagePickerControllerQualityType640x480;
        }
        
        // 代理
        imagePicker.delegate = self;
        //3. 显示
        [self presentViewController:imagePicker animated:YES completion:nil];
       
    
    代理方法

    不管是拍照录像,还是取照片都会走代理方法,存直接调用C语言方法

    //前面两个只会调用一个☝️,两个都实现只会调用第二个
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0);
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
    

    去照片时,如果调用的是第二个方法,可以通过字典和相关的key,取得对应的图片或video。例如: UIImagePickerControllerOriginalImage
    存图片和video 就比较简单,直接调用C语言函数:

    //存图片,传path,和回调事件(判断是否存取成功),不传也行
    // 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;
    
    //以下都是存video的,只是最后的方法,有回调事件(判断是否存取成功)
    // 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;
    

    这个东西比较简单。API 也比较简单。

    相关文章

      网友评论

          本文标题:iOS UIImagePickerController 录像和拍

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