ios 切换摄像头

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

//先声明两个属性,遵循两个协议(协议可能非必要,视具体开发而定)
//    
//    @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;

你可能感兴趣的:(ios 切换摄像头)