ios开发iOS DeveloperiOS 视频图像相关

ios如何录制视频保存和编辑视频

2017-03-21  本文已影响2107人  _我和你一样

如何录制视频

我们使用UIImagePickerController类设置媒体类型为视频

凡是涉及到与硬件交互,保险的做法是先判断是否可用.

我们使用UIImagePickerController的类方法来判断相机是否可用:判断可用的方法如下:

-(BOOL)isCramaAvialable{
    //相机是否可用
   return  [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
//    //后置相机是否可用
//    [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
//    //前置相机是否可用
//    [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
//    //后置闪光灯是否可用
//    [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear];
//    //前置闪光灯是否可用
//    [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceFront];
}

判断指定资源类型下的媒体类型是否可用
这里的资源类型是指 相机还是图库等
媒体类型是指 是图片还是视频 视频选用movie 而不是video 请注意

-(BOOL)isAvalableMediaType:(CFStringRef)mediaType sourcType:(UIImagePickerControllerSourceType)soureType{
    NSArray *array =  [UIImagePickerController availableMediaTypesForSourceType:soureType];
    NSLog(@"%@",array);
    return [array containsObject:(__bridge NSString *)mediaType];
}

第一步我们配置的代码这一这么写:

   //1.判断相机是否可用 视频是否可用
    if (![self isCramaAvialable]) {
     UIAlertController *alert =  [UIAlertController alertControllerWithTitle:@"相机不可用" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
        return;
    }

配置UIImagePickerController 并present出来

我们需要在这里配置PickerController的资源类型/媒体类型等,闪光灯,录制时间,视频质量,都可以在这里设置.

-(void)configPicker{
    self.pickerContr = [[UIImagePickerController alloc]init];
    //设置资源类型
    [self.pickerContr setSourceType:UIImagePickerControllerSourceTypeCamera];
    if ([self isAvalableMediaType:kUTTypeMovie sourcType:UIImagePickerControllerSourceTypeCamera]) {
        //配置媒体类型
         [self.pickerContr setMediaTypes:@[(__bridge NSString *)kUTTypeMovie]];
        //配置视频质量
        [self.pickerContr setVideoQuality:UIImagePickerControllerQualityTypeHigh];
        //设置最大录制时间
        [self.pickerContr setVideoMaximumDuration:10];
        //设置代理
        self.pickerContr.delegate = self;
        [self presentViewController:self.pickerContr animated:YES completion:nil];
    }else{
        UIAlertController *alert =  [UIAlertController alertControllerWithTitle:@"视频不可用" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

保存视频

这一步已经可以进行视频录制了,但是还没对拿到的视频数据尽心处理.刚才我们设置代理,视频的处理需要在代理的回调方法里进行;除了取消的代理方法,我们还是实现下面的代理方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;

在这里可以获取视频的相关信息,我们主要是拿到视频路径并保存.
保存视频在ios8之后,需要用到PHPhotoLibrary类,需要引入这个头文件#import <Photos/Photos.h>
需要调用PHAssetChangeRequest 的类方法creationRequestForAssetFromVideoAtFileURL
保存视频,注意这个方法只能在performChanges方法中调用,否则会崩溃
将视频保存到图库的代码如下:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIVideoEditorController *videoEditor = nil;
    if ([info[UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kUTTypeMovie]) {
        NSURL *URL =  info[UIImagePickerControllerMediaURL];
        //ios 9.0废弃
//        ALAssetsLibrary *lib = [[ALAssetsLibrary alloc]init];
//        [lib writeVideoAtPathToSavedPhotosAlbum:URL completionBlock:nil];
//        "Use creationRequestForAssetFromVideoAtFilePath: on PHAssetChangeRequest from the Photos framework to create a new asset instead"
        
        //ios 8.0+ 调用另外一个方法也可以,error也可以要传NULL
        NSError *error;
        [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
             [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:URL];
            NSLog(@"%@",error);
        } error:&error];
    [picker dismissViewControllerAnimated:YES completion:^{
        [self presentViewController:videoEditor animated:YES completion:nil];
    }];
}

编辑并保存视频

截止到上一步我们已经完成了视频的录制和保存.保存视频需要用到UIVideoEditorController类,给他视频的路径,并present就可以出现编辑界面了,如果要保存结果,同样要给他设置结果.我们在上一个代理方法中继续编辑,代码如下:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIVideoEditorController *videoEditor = nil;
    if ([info[UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kUTTypeMovie]) {
        NSURL *URL =  info[UIImagePickerControllerMediaURL];
        //ios 9.0废弃
//        ALAssetsLibrary *lib = [[ALAssetsLibrary alloc]init];
//        [lib writeVideoAtPathToSavedPhotosAlbum:URL completionBlock:nil];
//        "Use creationRequestForAssetFromVideoAtFilePath: on PHAssetChangeRequest from the Photos framework to create a new asset instead"
        
        //ios 8.0+
        NSError *error;
        [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
             [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:URL];
            NSLog(@"%@",error);
        } error:&error];
        
        if([UIVideoEditorController canEditVideoAtPath:URL.path]){
            videoEditor = [[UIVideoEditorController alloc]init];
            videoEditor.videoPath = URL.path;
            videoEditor.delegate = self;
        }
    }
    
    [picker dismissViewControllerAnimated:YES completion:^{
        [self presentViewController:videoEditor animated:YES completion:nil];
    }];
    
}

//下面这个是视频编辑控制器的代理方法
- (void)videoEditorController:(UIVideoEditorController *)editor didSaveEditedVideoToPath:(NSString *)editedVideoPath{
    NSLog(@"%@",editedVideoPath);
    
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:editedVideoPath]];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        if (success) {
            NSLog(@"保存成功");
        }
        
        if (error) {
            NSLog(@"%@",error);
        }
    }];
    
    [editor dismissViewControllerAnimated:YES completion:nil];
}

到这里,你已经学会了如果进行视频的录制保存和编辑了.

上一篇 下一篇

猜你喜欢

热点阅读