iOS使用UIImagePickerController录制视频

2017-07-03  本文已影响1556人  peterking

由于项目需求:在聊天中需在添加一个录制和播放小视频的功能。下面主要讲的使用苹果提供的UIImagePickerController进行视频的录制。

第一步:需要在plist文件中添加3个权限

1、Privacy - Camera Usage Description 使用相机
2、Privacy - Microphone Usage Description 使用麦克风
3、Privacy - Photo Library Usage Description 使用相册

第二步:导入静态库 如下图所示

screenshot.png

第三步:代码

1、需要导入的三个头文件 并为当前控制器添加代理UIImagePickerControllerDelegate,UINavigationControllerDelegate

#import <Photos/Photos.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <AssetsLibrary/AssetsLibrary.h>

2、需要判断当前设备是否可以拍摄和拥有拍摄权限以及对拍摄的一些参数进行的设置

UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    
       //判断是否可以拍摄
    if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //判断是否拥有拍摄权限
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
            return;
        }
        
        //拍摄
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        //录制的类型 下面为视频
        imagePicker.mediaTypes=@[(NSString*)kUTTypeMovie];
        
        //录制的时长
        imagePicker.videoMaximumDuration=10.0;
        
        //模态视图的弹出效果
        imagePicker.modalPresentationStyle=UIModalPresentationOverFullScreen;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }

3、视频录制完成进入代理方法创建路径获得视频


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    
    //返回的媒体类型是照片或者视频
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        //照片的处理
        [picker dismissViewControllerAnimated:YES completion:^{
            UIImage *img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        }];
        
    }else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
        
        //视频的处理
        [picker dismissViewControllerAnimated:YES completion:^() {
            
            //文件管理器
            NSFileManager* fm = [NSFileManager defaultManager];
            
            //创建视频的存放路径
            NSString * path = [NSString stringWithFormat:@"%@/tmp/video%.0f.merge.mp4", NSHomeDirectory(), [NSDate timeIntervalSinceReferenceDate] * 1000];
            NSURL *mergeFileURL = [NSURL fileURLWithPath:path];
            videoUrl = mergeFileURL;
            
            //通过文件管理器将视频存放的创建的路径中
            [fm copyItemAtURL:[info objectForKey:UIImagePickerControllerMediaURL] toURL:mergeFileURL error:nil];
            AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
            
            //根据AVURLAsset得出视频的时长
            CMTime   time = [asset duration];
            int seconds = ceil(time.value/time.timescale);
            NSString *videoTime = [NSString stringWithFormat:@"%d",seconds];
  
            //可以根据需求判断是否需要将录制的视频保存到系统相册中
//            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
//            NSURL *recordedVideoURL= [info objectForKey:UIImagePickerControllerMediaURL];
//            
//            if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]) {
//                [library writeVideoAtPathToSavedPhotosAlbum:recordedVideoURL
//                                            completionBlock:^(NSURL *assetURL, NSError *error){
//                                                
//                                            }];
//            }
            
            }];
    }
}


上一篇下一篇

猜你喜欢

热点阅读