欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ios 切换摄像头

程序员文章站 2022-07-01 15:28:46
...

如何在ios开发中,使用相机时切换摄像头

//先声明两个属性,遵循两个协议(协议可能非必要,视具体开发而定)
//    <AVCaptureVideoDataOutputSampleBufferDelegate,AVCaptureAudioDataOutputSampleBufferDelegate>
//    @property (nonatomic, strong) AVCaptureSession *captureSession;
//    @property (nonatomic, strong) AVCaptureDeviceInput *currentVideoDeviceInput;
   
   // 1.获取当前设备方向
   AVCaptureDevicePosition curPosition = _currentVideoDeviceInput.device.position;
   
   // 2.获取需要改变的方向
   AVCaptureDevicePosition togglePosition = curPosition == AVCaptureDevicePositionFront?AVCaptureDevicePositionBack:AVCaptureDevicePositionFront;
   
   // 3.获取改变的摄像头设备
   AVCaptureDevice *toggleDevice = [self getVideoDevice:togglePosition];
   
   // 4.获取改变的摄像头输入设备
   AVCaptureDeviceInput *toggleDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:toggleDevice error:nil];
   
   // 5.移除之前摄像头输入设备
   [_captureSession removeInput:_currentVideoDeviceInput];
   
   // 6.添加新的摄像头输入设备
   [_captureSession addInput:toggleDeviceInput];
   
   // 7.记录当前摄像头输入设备
   _currentVideoDeviceInput = toggleDeviceInput;