您好,登錄后才能下訂單哦!
本篇文章為大家展示了原生AVCapture怎么在iOS中使用,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
概述:
可用于音頻、二維碼、拍照、錄制視頻 (均可自定義界面)
常見的輸出信號:
AVCaptureAudioDataOutput 音頻輸出
AVCaptureFileOutput 文本輸出
AVCaptureMetadataOutput 二維碼 條形碼…
AVCaptureStillImageOutput 拍照
AVCaptureMovieFileOutput 錄制視頻(不能實現暫停錄制和定義視頻文件類型)
AVCaptureVideoDataOutput + AVCaptureAudioDataOutput 錄制視頻的靈活性更強(能實現暫停錄制和定義視頻文件類型)
AVCaptureMovieFileOutput輸出流實現視頻錄制
初始化會話層
-(void)sessionConfiguration{ //初始化一個會話 session = [[AVCaptureSession alloc] init]; [session setSessionPreset:AVCaptureSessionPresetMedium]; //創建視頻設備 AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //根據設備創建輸入信號 deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil]; //添加 輸出設備 movieFile self.deviceMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; [session beginConfiguration]; //session添加設備輸入信號 if ([session canAddInput:deviceInput]) { [session addInput:deviceInput]; } //session添加設備輸出信號 if ([session canAddOutput:self.deviceMovieFileOutput]) { [session addOutput:self.deviceMovieFileOutput]; } [session commitConfiguration]; }
創建預覽圖層
-(void)embedLayerWithView:(UIView *)view{ if (session == nil) { return; } videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session]; //設置圖層的大小 videoPreviewLayer.frame = view.bounds; videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; [view.layer addSublayer:videoPreviewLayer]; [session startRunning]; }
錄制視頻
-(void)takePhoto:(NSURL *)fileURL{ [self.deviceMovieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self]; }
結束錄制
-(UIImageView *)finishRecord:(UIView *)view isAnewRecording:(BOOL)anewRecording{ gifImageView = [[UIImageView alloc] initWithFrame:view.bounds]; [view addSubview:gifImageView]; isAnewRecording = anewRecording; //存儲是否重新錄制 //停止錄制(停止錄制后做代理方法) [self.deviceMovieFileOutput stopRecording]; return gifImageView; }
拍攝視頻保存路徑
+(NSString *)getVideoSaveFilePath{ NSString*documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [documentPath stringByAppendingPathComponent:@"video.mp4"]; return filePath; }
會話層啟動和關閉
-(void)startCamera{ [session startRunning]; } -(void)stopCamera{ [session stopRunning]; }
代理方法
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{ NSLog(@"完成錄制"); NSLog(@"outputFileURL = %@",outputFileURL); //**重新錄制**// if (isAnewRecording) { //**刪除視頻文件**// NSFileManager *manager = [NSFileManager defaultManager]; [manager removeItemAtPath:outputFileURL.absoluteString error:nil]; } //**不取消錄制**// else{ //**獲取視頻時長**// AVURLAsset *avUrl = [AVURLAsset URLAssetWithURL:outputFileURL options:nil]; CMTime time = [avUrl duration]; int seconds = ceil(time.value/time.timescale); NSLog(@"seconds = %d",seconds); if ([self.delegate respondsToSelector:@selector(videoDuration:)]) { [self.delegate videoDuration:seconds]; } if ([self.delegate respondsToSelector:@selector(playerVideo:)]) { [self.delegate playerVideo:outputFileURL.absoluteString]; } } }
上述內容就是原生AVCapture怎么在iOS中使用,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。