您的位置:首页 > 其它

个人笔记 AVfoundation框架的简单实用 自定义录制视频界面

2014-12-12 14:33 489 查看
一、创建拍摄界面 

首先需要声明4个变量

1.

录制视频是基于会话的 声明一个会话作为 所以动作的载体

 self.session=[[AVCaptureSessionalloc]init];
//设置画面质量
    [self.sessionsetSessionPreset:AVCaptureSessionPresetLow];
2.声明一个主体设备 说明这个设备是拍摄视频的

  self.captureDevice=[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
3.声明一个输入设备 这个设备是基于 主体设备的

    self.inputDevice=[AVCaptureDeviceInputdeviceInputWithDevice:self.captureDeviceerror:nil];

    //判断会话可否加入设备

    if ([self.sessioncanAddInput:self.inputDevice]
)
    {

        [self.sessionaddInput:self.inputDevice];
    }
4.这一段是设置拍摄界面的frame 的

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayeralloc]
initWithSession:self.session];

    [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    
   CALayer *rootLayer = [[selfview]
layer];
    [rootLayersetMasksToBounds:YES];
    [previewLayersetFrame:CGRectMake(-70,0,
rootLayer.bounds.size.height, rootLayer.bounds.size.height)];
    [rootLayerinsertSublayer:previewLayer
atIndex:0];
5.声明一个输出设备

  self.movieFileOutput=[[AVCaptureMovieFileOutputalloc]init];

6.将输出设备添加到会话中

    [self.sessionaddOutput:self.movieFileOutput];

7.启动设备

   [self.sessionstartRunning];
这样设备就运行了 但此时 还没有进行拍摄 
要进行拍摄 需要引入 代理 

AVCaptureFileOutputRecordingDelegate

二、然后在你拍摄按钮 加入点击方法 

- (void) startRecording

{

     NSString *filePath = [BMDAOgetVideoSaveFilePathString];//这个是设置
拍摄完毕时候视频的存储位置 

    [self.movieFileOutputstartRecordingToOutputFileURL:[NSURLfileURLWithPath:filePath]
recordingDelegate:self];

}
代码中加入代理方法

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL
*)fileURL fromConnections:(NSArray *)connections

{

    

    NSLog(@"start record video");

    
}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL
*)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error

{

    

    ALAssetsLibrary *library = [[ALAssetsLibraryalloc]
init];

    

    //将临时文件夹中的视频文件复制到
照片文件夹中,以便存取

    [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL

     
                               completionBlock:^(NSURL *assetURL,NSError
*error) {

                                    
                                   if (error) {

//                                      

                                        
                                    }

                                    

下一篇说闪光灯和切换摄像头
                                   else
                                        [WaitVCshowWaitViewWithMessage:[assetURL
path]];

                                    
                                }];

    
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息