亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

iOS AVCaptureSession實現視頻錄制功能

2019-10-21 18:41:15
字體:
來源:轉載
供稿:網友

本文實例為大家分享了AVCaptureSession實現視頻錄制功能的具體代碼,供大家參考,具體內容如下

#import "RecordingVideoViewController.h" #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h>  @interface RecordingVideoViewController ()<AVCaptureFileOutputRecordingDelegate>  //會話 負責輸入和輸出設備之間的數據傳遞 @property (strong,nonatomic) AVCaptureSession  *captureSession; //設備輸入 負責從AVCaptureDevice獲得輸入數據 @property (strong,nonatomic) AVCaptureDeviceInput  *videoCaptureDeviceInput; @property (strong,nonatomic) AVCaptureDeviceInput  *audioCaptureDeviceInput; //視頻輸出流 @property (strong,nonatomic) AVCaptureMovieFileOutput  *captureMovieFileOutput; //相機拍攝預覽圖層 @property (strong,nonatomic) AVCaptureVideoPreviewLayer  *captureVideoPreviewLayer;  //自定義UI控件容器 @property (strong,nonatomic) UIView  *viewContainer; //聚焦圖標 @property (strong,nonatomic) UIImageView  *focusCursor; //錄制時長 @property (strong,nonatomic) UILabel  *timeLabel; //切換前后攝像頭 @property (strong,nonatomic) UIButton  *switchCameraBtn; //改變焦距 @property (strong,nonatomic) UIButton  *scaleBtn; //計時器 @property (strong,nonatomic) NSTimer  *timer;   @end  @implementation RecordingVideoViewController {  @private   NSInteger _num;   CGFloat _kCameraScale; }   - (UIView *)viewContainer {   if (!_viewContainer) {     _viewContainer = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];          UIButton *takeButton = [UIButton buttonWithType:UIButtonTypeCustom];     takeButton.backgroundColor = [UIColor redColor];     [takeButton setTitle:@"start" forState:UIControlStateNormal];     [takeButton addTarget:self action:@selector(takeButtonClick:) forControlEvents:UIControlEventTouchUpInside];             _timeLabel = [[UILabel alloc] init];     _timeLabel.textColor = [UIColor redColor];     _timeLabel.textAlignment = NSTextAlignmentCenter;     _timeLabel.font = [UIFont boldSystemFontOfSize:20];     _timeLabel.text = @"00:00";               _switchCameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];     [_switchCameraBtn setTitle:@"switch" forState:UIControlStateNormal];     _switchCameraBtn.backgroundColor = [UIColor redColor];     [_switchCameraBtn addTarget:self action:@selector(switchCameraBtnClick) forControlEvents:UIControlEventTouchUpInside];          _scaleBtn = [UIButton buttonWithType:UIButtonTypeCustom];     [_scaleBtn setTitle:@"1X" forState:UIControlStateNormal];     _scaleBtn.backgroundColor = [UIColor redColor];     [_scaleBtn addTarget:self action:@selector(scaleBtnClick:) forControlEvents:UIControlEventTouchUpInside];          [_viewContainer addSubview:takeButton];     [_viewContainer addSubview:_timeLabel];     [_viewContainer addSubview:_scaleBtn];     [_viewContainer addSubview:_switchCameraBtn];     [takeButton mas_makeConstraints:^(MASConstraintMaker *make) {       make.size.mas_equalTo(CGSizeMake(60, 40));       make.centerX.mas_equalTo(_viewContainer);       make.bottom.mas_equalTo(_viewContainer).offset(-64);     }];     [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {       make.centerX.mas_equalTo(_viewContainer);       make.height.mas_equalTo(@30);       make.top.mas_equalTo(_viewContainer);     }];     [_scaleBtn mas_makeConstraints:^(MASConstraintMaker *make) {       make.size.mas_equalTo(CGSizeMake(60, 40));       make.left.mas_equalTo(_viewContainer).offset(10);       make.top.mas_equalTo(_viewContainer);     }];     [_switchCameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {       make.size.mas_equalTo(CGSizeMake(60, 40));       make.top.mas_equalTo(_viewContainer);       make.right.mas_equalTo(_viewContainer).offset(-10);     }];          _focusCursor = [[UIImageView alloc] init];     kBorder(_focusCursor, 1, [UIColor yellowColor]);     _focusCursor.alpha = 0;     [_viewContainer addSubview:self.focusCursor];     [_focusCursor mas_makeConstraints:^(MASConstraintMaker *make) {       make.size.mas_equalTo(CGSizeMake(40, 40));       make.center.mas_equalTo(_viewContainer);     }];    }   return _viewContainer; }  - (void)viewDidLoad {   [super viewDidLoad];      self.title = @"視頻錄制";   _kCameraScale = 1.0f;   //初始化會話對象   _captureSession = [[AVCaptureSession alloc] init];   if ([_captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {     _captureSession.sessionPreset = AVCaptureSessionPreset1280x720;   }         NSError *error = nil;    //獲取視頻輸入對象   AVCaptureDevice *videoCaptureDevice = [self cameraDeviceWithPosition:(AVCaptureDevicePositionBack)];   if (!videoCaptureDevice) {     NSLog(@"獲取后置攝像頭失敗!");     return;   }   _videoCaptureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:videoCaptureDevice error:&error];   if (error) {     NSLog(@"取得視頻設備輸入對象時出錯");     return;   }         //獲取音頻輸入對象   AVCaptureDevice *audioCatureDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];   _audioCaptureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioCatureDevice error:&error];   if (error) {     NSLog(@"取得音頻設備輸入對象時出錯");     return;   }      //初始化設備輸出對象   _captureMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];      //將設備輸入添加到會話中   if ([_captureSession canAddInput:_videoCaptureDeviceInput]) {     [_captureSession addInput:_videoCaptureDeviceInput];     [_captureSession addInput:_audioCaptureDeviceInput];          //防抖功能     AVCaptureConnection *captureConnection = [_captureMovieFileOutput connectionWithMediaType:AVMediaTypeAudio];     if ([captureConnection isVideoStabilizationSupported]) {       captureConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;     }   }      //將設備輸出添加到會話中   if ([_captureSession canAddOutput:_captureMovieFileOutput]) {     [_captureSession addOutput:_captureMovieFileOutput];   }         //創建視頻預覽圖層   _captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];   self.viewContainer.layer.masksToBounds = YES;   _captureVideoPreviewLayer.frame = self.viewContainer.bounds;   _captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;   [self.view.layer addSublayer:_captureVideoPreviewLayer];      //顯示自定義控件   [self.view addSubview:self.viewContainer];      //添加點按聚焦手勢   UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapScreen:)];   [self.viewContainer addGestureRecognizer:tapGesture];    }  -(void)viewDidAppear:(BOOL)animated{   [super viewDidAppear:animated];   [self.captureSession startRunning]; }  -(void)viewDidDisappear:(BOOL)animated{   [super viewDidDisappear:animated];   [self.captureSession stopRunning];   [self.timer invalidate];   self.timer = nil; }  - (void)viewWillDisappear:(BOOL)animated {   [super viewWillDisappear:animated];   [self.captureVideoPreviewLayer setAffineTransform:CGAffineTransformMakeScale(1, 1)]; }  - (void)didReceiveMemoryWarning {   [super didReceiveMemoryWarning]; }  //開始 + 暫停錄制 - (void)takeButtonClick:(UIButton *)sender {   if ([self.captureMovieFileOutput isRecording]) {     [self.captureMovieFileOutput stopRecording];          [self.navigationController popViewControllerAnimated:YES];        } else {     AVCaptureConnection *captureConnection = [self.captureMovieFileOutput connectionWithMediaType:AVMediaTypeVideo];     captureConnection.videoOrientation = [self.captureVideoPreviewLayer connection].videoOrientation;          NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Movie.mov"];     NSLog(@"%@",filePath);     [self.captureMovieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] recordingDelegate:self];               self.switchCameraBtn.hidden = YES;          sender.backgroundColor = [UIColor greenColor];     [sender setTitle:@"stop" forState:UIControlStateNormal];          self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];     [self.timer setFireDate:[NSDate distantPast]];   } }  //切換攝像頭 - (void)switchCameraBtnClick {   AVCaptureDevicePosition currentPosition = self.videoCaptureDeviceInput.device.position;   AVCaptureDevicePosition toPosition;   if (currentPosition == AVCaptureDevicePositionUnspecified ||     currentPosition == AVCaptureDevicePositionFront) {     toPosition = AVCaptureDevicePositionBack;   } else {     toPosition = AVCaptureDevicePositionFront;   }      AVCaptureDevice *toCapturDevice = [self cameraDeviceWithPosition:toPosition];   if (!toCapturDevice) {     NSLog(@"獲取要切換的設備失敗");     return;   }      NSError *error = nil;   AVCaptureDeviceInput *toVideoDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:toCapturDevice error:&error];   if (error) {     NSLog(@"獲取要切換的設備輸入失敗");     return;   }      //改變會話配置   [self.captureSession beginConfiguration];      [self.captureSession removeInput:self.videoCaptureDeviceInput];   if ([self.captureSession canAddInput:toVideoDeviceInput]) {     [self.captureSession addInput:toVideoDeviceInput];          self.videoCaptureDeviceInput = toVideoDeviceInput;   }   //提交會話配置   [self.captureSession commitConfiguration]; }   //點按手勢 - (void)tapScreen:(UITapGestureRecognizer *)tap {   CGPoint point = [tap locationInView:self.viewContainer];      //將界面point對應到攝像頭point   CGPoint cameraPoint = [self.captureVideoPreviewLayer captureDevicePointOfInterestForPoint:point];    //設置聚光動畫   self.focusCursor.center = point;   self.focusCursor.transform = CGAffineTransformMakeScale(1.5, 1.5);   self.focusCursor.alpha = 1.0f;   [UIView animateWithDuration:1 animations:^{     self.focusCursor.transform = CGAffineTransformIdentity;   } completion:^(BOOL finished) {     self.focusCursor.alpha = 0.0f;    }];      //設置聚光點坐標   [self focusWithMode:AVCaptureFocusModeAutoFocus exposureMode:AVCaptureExposureModeAutoExpose atPoint:cameraPoint];  }   /**設置聚焦點*/ -(void)focusWithMode:(AVCaptureFocusMode)focusMode exposureMode:(AVCaptureExposureMode)exposureMode atPoint:(CGPoint)point{      AVCaptureDevice *captureDevice= [self.videoCaptureDeviceInput device];   NSError *error = nil;   //設置設備屬性必須先解鎖 然后加鎖   if ([captureDevice lockForConfiguration:&error]) {          if ([captureDevice isFocusModeSupported:focusMode]) {       [captureDevice setFocusMode:focusMode];     }     if ([captureDevice isFocusPointOfInterestSupported]) {       [captureDevice setFocusPointOfInterest:point];     }     //    //曝光     //    if ([captureDevice isExposureModeSupported:exposureMode]) {     //      [captureDevice setExposureMode:exposureMode];     //    }     //    if ([captureDevice isExposurePointOfInterestSupported]) {     //      [captureDevice setExposurePointOfInterest:point];     //    }     //    //閃光燈模式     //    if ([captureDevice isFlashModeSupported:AVCaptureFlashModeAuto]) {     //      [captureDevice setFlashMode:AVCaptureFlashModeAuto];     //    }          //加鎖     [captureDevice unlockForConfiguration];        }else{     NSLog(@"設置設備屬性過程發生錯誤,錯誤信息:%@",error.localizedDescription);   } }    //調整焦距 -(void)scaleBtnClick:(UIButton *)sender {   _kCameraScale += 0.5;   if(_kCameraScale > 3.0) {     _kCameraScale = 1.0;   }   //改變焦距   AVCaptureDevice *videoDevice = self.videoCaptureDeviceInput.device;   NSError *error = nil;   if ([videoDevice lockForConfiguration:&error]) {          [videoDevice setVideoZoomFactor:_kCameraScale];          [videoDevice unlockForConfiguration];          [sender setTitle:[NSString stringWithFormat:@"%lgX",_kCameraScale] forState:UIControlStateNormal];      [CATransaction begin];     [CATransaction setAnimationDuration:0.25];     [self.captureVideoPreviewLayer setAffineTransform:CGAffineTransformMakeScale(_kCameraScale, _kCameraScale)];     [CATransaction commit];        } else {     NSLog(@"修改設備屬性失敗!")   } }    #pragma mark -------- AVCaptureFileOutputRecordingDelegate ---------- - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections {   NSLog(@"開始錄制"); }  - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {   NSLog(@"錄制結束");   ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];   [assetsLibrary writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {     if (error) {       NSLog(@"保存視頻到相簿過程中發生錯誤,錯誤信息:%@",error.localizedDescription);     }   }]; }  //錄制計時 - (void)timeAction {   self.timeLabel.text = [NSString stringWithFormat:@"%.2ld:%.2ld",_num/60,_num%60];   _num ++; }   /**取得指定位置的攝像頭*/ - (AVCaptureDevice *)cameraDeviceWithPosition:(AVCaptureDevicePosition )position{   NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];   for (AVCaptureDevice *camera in cameras) {     if ([camera position] == position) {       return camera;     }   }   return nil; }   @end 

參考代碼:

#import "VideoTestViewController.h" #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h>  typedef void(^PropertyChangeBlock)(AVCaptureDevice *captureDevice);  @interface VideoTestViewController ()<AVCaptureFileOutputRecordingDelegate>//視頻文件輸出代理  @property (strong,nonatomic) AVCaptureSession *captureSession;//負責輸入和輸出設備之間的數據傳遞 @property (strong,nonatomic) AVCaptureDeviceInput *captureDeviceInput;//負責從AVCaptureDevice獲得輸入數據 @property (strong,nonatomic) AVCaptureMovieFileOutput *captureMovieFileOutput;//視頻輸出流 @property (strong,nonatomic) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;//相機拍攝預覽圖層  @property (assign,nonatomic) BOOL enableRotation;//是否允許旋轉(注意在視頻錄制過程中禁止屏幕旋轉) @property (assign,nonatomic) CGRect *lastBounds;//旋轉的前大小 @property (assign,nonatomic) UIBackgroundTaskIdentifier backgroundTaskIdentifier;//后臺任務標識 @property (strong,nonatomic) UIView *viewContainer; @property (strong,nonatomic) UIButton *takeButton;//拍照按鈕 @property (strong,nonatomic) UIImageView *focusCursor; //聚焦光標   @end  @implementation VideoTestViewController  #pragma mark - 控制器視圖方法 - (void)viewDidLoad {   [super viewDidLoad]; }  -(void)viewWillAppear:(BOOL)animated{   [super viewWillAppear:animated];      //初始化會話   _captureSession=[[AVCaptureSession alloc]init];   if ([_captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {//設置分辨率     _captureSession.sessionPreset=AVCaptureSessionPreset1280x720;   }   //獲得輸入設備   AVCaptureDevice *captureDevice=[self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];//取得后置攝像頭   if (!captureDevice) {     NSLog(@"取得后置攝像頭時出現問題.");     return;   }   //添加一個音頻輸入設備   AVCaptureDevice *audioCaptureDevice=[[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];         NSError *error=nil;   //根據輸入設備初始化設備輸入對象,用于獲得輸入數據   _captureDeviceInput=[[AVCaptureDeviceInput alloc]initWithDevice:captureDevice error:&error];   if (error) {     NSLog(@"取得設備輸入對象時出錯,錯誤原因:%@",error.localizedDescription);     return;   }   AVCaptureDeviceInput *audioCaptureDeviceInput=[[AVCaptureDeviceInput alloc]initWithDevice:audioCaptureDevice error:&error];   if (error) {     NSLog(@"取得設備輸入對象時出錯,錯誤原因:%@",error.localizedDescription);     return;   }   //初始化設備輸出對象,用于獲得輸出數據   _captureMovieFileOutput=[[AVCaptureMovieFileOutput alloc]init];      //將設備輸入添加到會話中   if ([_captureSession canAddInput:_captureDeviceInput]) {     [_captureSession addInput:_captureDeviceInput];     [_captureSession addInput:audioCaptureDeviceInput];     AVCaptureConnection *captureConnection=[_captureMovieFileOutput connectionWithMediaType:AVMediaTypeVideo];     if ([captureConnection isVideoStabilizationSupported ]) {       captureConnection.preferredVideoStabilizationMode=AVCaptureVideoStabilizationModeAuto;     }   }      //將設備輸出添加到會話中   if ([_captureSession canAddOutput:_captureMovieFileOutput]) {     [_captureSession addOutput:_captureMovieFileOutput];   }      //創建視頻預覽層,用于實時展示攝像頭狀態   _captureVideoPreviewLayer=[[AVCaptureVideoPreviewLayer alloc]initWithSession:self.captureSession];      CALayer *layer=self.viewContainer.layer;   layer.masksToBounds=YES;      _captureVideoPreviewLayer.frame=layer.bounds;   _captureVideoPreviewLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//填充模式   //將視頻預覽層添加到界面中   //[layer addSublayer:_captureVideoPreviewLayer];   [layer insertSublayer:_captureVideoPreviewLayer below:self.focusCursor.layer];      _enableRotation=YES;   [self addNotificationToCaptureDevice:captureDevice];   [self addGenstureRecognizer]; }  -(void)viewDidAppear:(BOOL)animated{   [super viewDidAppear:animated];   [self.captureSession startRunning]; }  -(void)viewDidDisappear:(BOOL)animated{   [super viewDidDisappear:animated];   [self.captureSession stopRunning]; }  - (void)didReceiveMemoryWarning {   [super didReceiveMemoryWarning]; }  -(BOOL)shouldAutorotate{   return self.enableRotation; }  ////屏幕旋轉時調整視頻預覽圖層的方向 //-(void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{ //  [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator]; ////  NSLog(@"%i,%i",newCollection.verticalSizeClass,newCollection.horizontalSizeClass); //  UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; //  NSLog(@"%i",orientation); //  AVCaptureConnection *captureConnection=[self.captureVideoPreviewLayer connection]; //  captureConnection.videoOrientation=orientation; // //} //屏幕旋轉時調整視頻預覽圖層的方向 -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{   AVCaptureConnection *captureConnection=[self.captureVideoPreviewLayer connection];   captureConnection.videoOrientation=(AVCaptureVideoOrientation)toInterfaceOrientation; } //旋轉后重新設置大小 -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{   _captureVideoPreviewLayer.frame=self.viewContainer.bounds; }  -(void)dealloc{   [self removeNotification]; } #pragma mark - UI方法 #pragma mark 視頻錄制 - (void)takeButtonClick:(UIButton *)sender {   //根據設備輸出獲得連接   AVCaptureConnection *captureConnection=[self.captureMovieFileOutput connectionWithMediaType:AVMediaTypeVideo];   //根據連接取得設備輸出的數據   if (![self.captureMovieFileOutput isRecording]) {     self.enableRotation=NO;     //如果支持多任務則則開始多任務     if ([[UIDevice currentDevice] isMultitaskingSupported]) {       self.backgroundTaskIdentifier=[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];     }     //預覽圖層和視頻方向保持一致     captureConnection.videoOrientation=[self.captureVideoPreviewLayer connection].videoOrientation;     NSString *outputFielPath=[NSTemporaryDirectory() stringByAppendingString:@"myMovie.mov"];     NSLog(@"save path is :%@",outputFielPath);     NSURL *fileUrl=[NSURL fileURLWithPath:outputFielPath];     [self.captureMovieFileOutput startRecordingToOutputFileURL:fileUrl recordingDelegate:self];   }   else{     [self.captureMovieFileOutput stopRecording];//停止錄制   } } #pragma mark 切換前后攝像頭 - (void)toggleButtonClick:(UIButton *)sender {   AVCaptureDevice *currentDevice=[self.captureDeviceInput device];   AVCaptureDevicePosition currentPosition=[currentDevice position];   [self removeNotificationFromCaptureDevice:currentDevice];   AVCaptureDevice *toChangeDevice;   AVCaptureDevicePosition toChangePosition=AVCaptureDevicePositionFront;   if (currentPosition==AVCaptureDevicePositionUnspecified||currentPosition==AVCaptureDevicePositionFront) {     toChangePosition=AVCaptureDevicePositionBack;   }   toChangeDevice=[self getCameraDeviceWithPosition:toChangePosition];   [self addNotificationToCaptureDevice:toChangeDevice];   //獲得要調整的設備輸入對象   AVCaptureDeviceInput *toChangeDeviceInput=[[AVCaptureDeviceInput alloc]initWithDevice:toChangeDevice error:nil];      //改變會話的配置前一定要先開啟配置,配置完成后提交配置改變   [self.captureSession beginConfiguration];   //移除原有輸入對象   [self.captureSession removeInput:self.captureDeviceInput];   //添加新的輸入對象   if ([self.captureSession canAddInput:toChangeDeviceInput]) {     [self.captureSession addInput:toChangeDeviceInput];     self.captureDeviceInput=toChangeDeviceInput;   }   //提交會話配置   [self.captureSession commitConfiguration];    }  #pragma mark - 視頻輸出代理 -(void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections{   NSLog(@"開始錄制..."); } -(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{   NSLog(@"視頻錄制完成.");   //視頻錄入完成之后在后臺將視頻存儲到相簿   self.enableRotation=YES;   UIBackgroundTaskIdentifier lastBackgroundTaskIdentifier=self.backgroundTaskIdentifier;   self.backgroundTaskIdentifier=UIBackgroundTaskInvalid;   ALAssetsLibrary *assetsLibrary=[[ALAssetsLibrary alloc]init];   [assetsLibrary writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {     if (error) {       NSLog(@"保存視頻到相簿過程中發生錯誤,錯誤信息:%@",error.localizedDescription);     }     if (lastBackgroundTaskIdentifier!=UIBackgroundTaskInvalid) {       [[UIApplication sharedApplication] endBackgroundTask:lastBackgroundTaskIdentifier];     }     NSLog(@"成功保存視頻到相簿.");   }];    }  #pragma mark - 通知 /**  * 給輸入設備添加通知  */ -(void)addNotificationToCaptureDevice:(AVCaptureDevice *)captureDevice{   //注意添加區域改變捕獲通知必須首先設置設備允許捕獲   [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) {     captureDevice.subjectAreaChangeMonitoringEnabled=YES;   }];   NSNotificationCenter *notificationCenter= [NSNotificationCenter defaultCenter];   //捕獲區域發生改變   [notificationCenter addObserver:self selector:@selector(areaChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:captureDevice]; } -(void)removeNotificationFromCaptureDevice:(AVCaptureDevice *)captureDevice{   NSNotificationCenter *notificationCenter= [NSNotificationCenter defaultCenter];   [notificationCenter removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:captureDevice]; } /**  * 移除所有通知  */ -(void)removeNotification{   NSNotificationCenter *notificationCenter= [NSNotificationCenter defaultCenter];   [notificationCenter removeObserver:self]; }  -(void)addNotificationToCaptureSession:(AVCaptureSession *)captureSession{   NSNotificationCenter *notificationCenter= [NSNotificationCenter defaultCenter];   //會話出錯   [notificationCenter addObserver:self selector:@selector(sessionRuntimeError:) name:AVCaptureSessionRuntimeErrorNotification object:captureSession]; }  /**  * 設備連接成功  *  * @param notification 通知對象  */ -(void)deviceConnected:(NSNotification *)notification{   NSLog(@"設備已連接..."); } /**  * 設備連接斷開  *  * @param notification 通知對象  */ -(void)deviceDisconnected:(NSNotification *)notification{   NSLog(@"設備已斷開."); } /**  * 捕獲區域改變  *  * @param notification 通知對象  */ -(void)areaChange:(NSNotification *)notification{   NSLog(@"捕獲區域改變..."); }  /**  * 會話出錯  *  * @param notification 通知對象  */ -(void)sessionRuntimeError:(NSNotification *)notification{   NSLog(@"會話發生錯誤."); }  #pragma mark - 私有方法  /**  * 取得指定位置的攝像頭  *  * @param position 攝像頭位置  *  * @return 攝像頭設備  */ -(AVCaptureDevice *)getCameraDeviceWithPosition:(AVCaptureDevicePosition )position{   NSArray *cameras= [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];   for (AVCaptureDevice *camera in cameras) {     if ([camera position]==position) {       return camera;     }   }   return nil; }  /**  * 改變設備屬性的統一操作方法  *  * @param propertyChange 屬性改變操作  */ -(void)changeDeviceProperty:(PropertyChangeBlock)propertyChange{   AVCaptureDevice *captureDevice= [self.captureDeviceInput device];   NSError *error;   //注意改變設備屬性前一定要首先調用lockForConfiguration:調用完之后使用unlockForConfiguration方法解鎖   if ([captureDevice lockForConfiguration:&error]) {     propertyChange(captureDevice);     [captureDevice unlockForConfiguration];   }else{     NSLog(@"設置設備屬性過程發生錯誤,錯誤信息:%@",error.localizedDescription);   } }  /**  * 設置閃光燈模式  *  * @param flashMode 閃光燈模式  */ -(void)setFlashMode:(AVCaptureFlashMode )flashMode{   [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) {     if ([captureDevice isFlashModeSupported:flashMode]) {       [captureDevice setFlashMode:flashMode];     }   }]; } /**  * 設置聚焦模式  *  * @param focusMode 聚焦模式  */ -(void)setFocusMode:(AVCaptureFocusMode )focusMode{   [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) {     if ([captureDevice isFocusModeSupported:focusMode]) {       [captureDevice setFocusMode:focusMode];     }   }]; } /**  * 設置曝光模式  *  * @param exposureMode 曝光模式  */ -(void)setExposureMode:(AVCaptureExposureMode)exposureMode{   [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) {     if ([captureDevice isExposureModeSupported:exposureMode]) {       [captureDevice setExposureMode:exposureMode];     }   }]; } /**  * 設置聚焦點  *  * @param point 聚焦點  */ -(void)focusWithMode:(AVCaptureFocusMode)focusMode exposureMode:(AVCaptureExposureMode)exposureMode atPoint:(CGPoint)point{   [self changeDeviceProperty:^(AVCaptureDevice *captureDevice) {     if ([captureDevice isFocusModeSupported:focusMode]) {       [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];     }     if ([captureDevice isFocusPointOfInterestSupported]) {       [captureDevice setFocusPointOfInterest:point];     }     if ([captureDevice isExposureModeSupported:exposureMode]) {       [captureDevice setExposureMode:AVCaptureExposureModeAutoExpose];     }     if ([captureDevice isExposurePointOfInterestSupported]) {       [captureDevice setExposurePointOfInterest:point];     }   }]; }  /**  * 添加點按手勢,點按時聚焦  */ -(void)addGenstureRecognizer{   UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapScreen:)];   [self.viewContainer addGestureRecognizer:tapGesture]; } -(void)tapScreen:(UITapGestureRecognizer *)tapGesture{   CGPoint point= [tapGesture locationInView:self.viewContainer];   //將UI坐標轉化為攝像頭坐標   CGPoint cameraPoint= [self.captureVideoPreviewLayer captureDevicePointOfInterestForPoint:point];   [self setFocusCursorWithPoint:point];   [self focusWithMode:AVCaptureFocusModeAutoFocus exposureMode:AVCaptureExposureModeAutoExpose atPoint:cameraPoint]; }  /**  * 設置聚焦光標位置  *  * @param point 光標位置  */ -(void)setFocusCursorWithPoint:(CGPoint)point{   self.focusCursor.center=point;   self.focusCursor.transform=CGAffineTransformMakeScale(1.5, 1.5);   self.focusCursor.alpha=1.0;   [UIView animateWithDuration:1.0 animations:^{     self.focusCursor.transform=CGAffineTransformIdentity;   } completion:^(BOOL finished) {     self.focusCursor.alpha=0;   }]; } @end 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品爱久久久久久久| 日韩精品免费在线| 亚洲国产成人精品女人久久久| 亚洲国产精品999| 国模私拍一区二区三区| 欧美激情成人在线视频| 日韩有码在线观看| 亚洲精品720p| 亚洲国产欧美在线成人app| 国产精品久久久久久久久久久久久| 美女少妇精品视频| 91精品国产高清自在线| 欧美国产亚洲精品久久久8v| 日韩精品免费电影| 日本精品va在线观看| 国产视频丨精品|在线观看| 97国产精品视频人人做人人爱| 精品久久香蕉国产线看观看gif| 国产成人在线亚洲欧美| 精品一区二区三区三区| 国产精品黄页免费高清在线观看| 中文字幕自拍vr一区二区三区| 久久不射热爱视频精品| 亚洲人成伊人成综合网久久久| 国产成人免费av电影| 一二美女精品欧洲| 久久久久成人网| 亚洲成人精品视频| 欧美最猛性xxxxx(亚洲精品)| 久久6免费高清热精品| 欧美性xxxxhd| 亚洲国产精彩中文乱码av| 国产999在线观看| 欧美高清视频一区二区| 国产精品免费久久久久久| 欧美综合第一页| 亚洲第一区中文字幕| 午夜精品久久久久久久白皮肤| 免费av在线一区| 欧美激情小视频| 国产欧美日韩综合精品| 久久亚洲精品网站| 国产成人精品电影| 91免费精品国偷自产在线| www高清在线视频日韩欧美| 久久久久久一区二区三区| www.亚洲免费视频| 久久综合久久88| 97精品久久久| 日本韩国欧美精品大片卡二| 亚洲色图日韩av| 日韩精品极品视频免费观看| 日韩中文字幕第一页| 欧美精品久久一区二区| 国产日韩换脸av一区在线观看| 久久免费视频这里只有精品| 91av视频在线免费观看| 国产亚洲精品高潮| 激情久久av一区av二区av三区| 国产午夜精品理论片a级探花| 欧美高跟鞋交xxxxxhd| 亚洲精品久久久久中文字幕二区| 日韩精品极品在线观看| 亚洲高清不卡av| 日韩欧美精品网址| 日韩欧美亚洲国产一区| 青青草99啪国产免费| 亚洲精品中文字幕女同| 久久久伊人日本| 亚洲免费一级电影| 国产成人午夜视频网址| 日韩有码在线观看| 欧美精品生活片| 亚洲自拍偷拍视频| 91精品视频网站| 播播国产欧美激情| 国产成人一区二区三区小说| 在线观看日韩视频| 91在线视频一区| 国产福利视频一区二区| 亚洲国产日韩欧美在线动漫| 久久亚洲精品成人| 亚洲第一色在线| 国产精品极品美女粉嫩高清在线| 亚洲午夜国产成人av电影男同| 亚洲色图25p| 国产99久久精品一区二区永久免费| 亚洲电影免费观看高清| 成人天堂噜噜噜| 奇米4444一区二区三区| 97精品久久久| 日韩在线视频观看| 国产精品一区二区久久| 91久久久久久久久久久| 亚洲欧美日韩中文在线制服| 18性欧美xxxⅹ性满足| 国产suv精品一区二区三区88区| 青青a在线精品免费观看| 欧美精品在线免费播放| 中文国产亚洲喷潮| 亚洲一区精品电影| 中文字幕亚洲情99在线| 午夜精品久久久久久99热| 日韩精品中文字幕在线| 欧美乱妇高清无乱码| 久久久久日韩精品久久久男男| 欧美影院成年免费版| 精品成人乱色一区二区| 国内成人精品视频| 欧美视频不卡中文| 亚洲经典中文字幕| 国内精品国产三级国产在线专| 欧美电影免费观看高清| 久久男人的天堂| 欧美xxxx14xxxxx性爽| 欧美另类99xxxxx| 98精品国产自产在线观看| 久久国产视频网站| 91精品国产综合久久香蕉| 欧美激情欧美激情在线五月| 欧美国产高跟鞋裸体秀xxxhd| 欧美日韩在线影院| 欧美精品videofree1080p| 亚洲性xxxx| 国产剧情日韩欧美| 午夜精品免费视频| 欧美疯狂性受xxxxx另类| 日韩一区二区久久久| 亚洲自拍偷拍色图| 在线日韩欧美视频| 96精品久久久久中文字幕| 亚洲wwwav| 成人网在线观看| 日本精品视频网站| 性欧美xxxx交| 国产精品久久久精品| 美日韩丰满少妇在线观看| 欧美激情三级免费| 欧美激情中文字幕乱码免费| 久久久伊人欧美| 91综合免费在线| 国产aⅴ夜夜欢一区二区三区| 欧美黑人狂野猛交老妇| 成人免费福利视频| 91中文字幕在线观看| 精品久久久免费| 亚洲精品久久久久久下一站| 成人免费福利在线| 国产精品一区二区三区久久| 欧美精品电影在线| 久久精品中文字幕电影| 欧美日韩国产精品一区二区不卡中文| 国产精品网红福利| 亚洲精品永久免费精品| 主播福利视频一区| 欧美成人精品在线视频| 韩国三级日本三级少妇99| 日韩欧美中文免费| 亚洲人成欧美中文字幕| 日韩av中文字幕在线播放| 日韩在线视频线视频免费网站| 91精品国产91久久久久久| 国产精品人人做人人爽| 久久人人97超碰精品888|