2017-09-23 17 views
0

그래서이 문제는 Xcode에서 사용자 정의 카메라를 만들려고하는데, 어떤 이유로 전면 카메라를 사용하도록 설정할 수 없습니다. 코드에서 내가 무엇을 바꿔도 뒤 카메라 만 사용하는 것처럼 보이고 누군가가 내 코드를 빠르게 살펴보고 내가 누락 된 부분이나 내가 간 곳이 있는지 확인할 수 있기를 바랬다. 잘못된. 어떤 도움이라도 대단히 감사 할 것입니다. 시간 내 주셔서 감사합니다.xcode의 사용자 정의 카메라, Swift 3

func SelectInputDevice() { 

let devices = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, 
       mediaType: AVMediaTypeVideo, position: .front) 
if devices?.position == AVCaptureDevicePosition.front { 

print(devices?.position) 
frontCamera = devices 

} 

currentCameraDevice = frontCamera 
do { 

    let captureDeviceInput = try AVCaptureDeviceInput(device: currentCameraDevice) 
    captureSession.addInput(captureDeviceInput) 

} catch { 

    print(error.localizedDescription) 

    } 

} 

여기는 frontCamera 및 currentCameraDevice가 AVCaptureDevice의 위치입니다.

답변

1

는 코드에서 누락 된 몇 가지가 보인다 : 새 장치를 추가하고 session.commitConfiguration()로 끝나는 전에 session.beginConfiguration()를 호출하여 세션을 다시 구성해야 입력 장치를 변경하려면

1). 또한 모든 변경 사항은 백그라운드 대기열에서 이루어져야합니다 (세션에 대해 생성했기를 바랍니다) 세션을 구성 할 때 UI가 차단되지 않도록해야합니다.

2) 세션을 확인하는 것이 더 안전 할 수 있습니다. 코드를 추가하기 전에 새 장치를 허용하기 전에 앞면 + 뒷면 구성이 허용되지 않으므로 session.canAddInput(captureDeviceInput) + 이전 장치 (뒷면 카메라)를 제거하십시오.

3) 또한 충돌을 사전에 방지하기 위해 작동하는 전면 카메라 (깨진 가능성이 있음)가 있는지 확인하는 것이 더 깔끔합니다. 시간에 대한

func switchCameraToFront() { 

    //session & sessionQueue are references to the capture session and its dispatch queue 
    sessionQueue.async { [unowned self] in 

     let currentVideoInput = self.videoDeviceInput //ref to current videoInput as setup in initial session config 

     let preferredPosition: AVCaptureDevicePosition = .front 
     let preferredDeviceType: AVCaptureDeviceType = .builtInWideAngleCamera 

     let devices = self.videoDeviceDiscoverySession.devices! 
     var newVideoDevice: AVCaptureDevice? = nil 

     // First, look for a device with both the preferred position and device type. Otherwise, look for a device with only the preferred position. 
     if let device = devices.filter({ $0.position == preferredPosition && $0.deviceType == preferredDeviceType }).first { 
      newVideoDevice = device 
     } 
     else if let device = devices.filter({ $0.position == preferredPosition }).first { 
      newVideoDevice = device 
     } 

     if let videoDevice = newVideoDevice { 
      do { 
       let videoDeviceInput = try AVCaptureDeviceInput(device: videoDevice) 

       self.session.beginConfiguration() 

       // Remove the existing device input first, since using the front and back camera simultaneously is not supported. 
       self.session.removeInput(currentVideoInput) 

       if self.session.canAddInput(videoDeviceInput) {            

        self.session.addInput(videoDeviceInput) 
        self.videoDeviceInput = videoDeviceInput 
       } 
       else { 
        //fallback to current device 
        self.session.addInput(self.videoDeviceInput); 
       } 
       self.session.commitConfiguration() 
      } 
      catch { 

      } 
     } 
    } 
} 
+0

환상적인 답변 감사 : 전면 카메라로 캡처 장치를 변경하는

전체 코드는 같을 것이다! – imjonu