2017-01-28 7 views
0

フロントカメラをバックカメラに変更する必要があります。私はボタンを作成しようとすると、カメラを変更するが、それを行う方法ではない。これは私のコードです(このコードはフロントカメラを実行します):フロントカメラを交換する

var captureSession = AVCaptureSession() 
var sessionOutput = AVCapturePhotoOutput() 
var sessionOutputSetting = AVCapturePhotoSettings(format:[AVVideoCodecKey:AVVideoCodecJPEG]) 

var previewLayer : AVCaptureVideoPreviewLayer? 


@IBOutlet var cameraView: UIView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    previewLayer?.frame = cameraView.bounds 
} 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 



    let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.unspecified) 
    for device in (deviceDiscoverySession?.devices)! { 
     if device.position == AVCaptureDevicePosition.back { 

      do { 
       let input = try AVCaptureDeviceInput(device: device) 
       if captureSession.canAddInput(input) { 
        captureSession.addInput(input) 

        if captureSession.canAddOutput(sessionOutput) { 
         captureSession.addOutput(sessionOutput) 
         previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
         previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
         previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait 
         cameraView.layer.addSublayer(previewLayer!) 

        } 
       } 

      } catch { 
       print("Error") 

      } 
     } 
    } 
    captureSession.startRunning() 
    print("") 


} 

申し訳ありませんが、私はまだ初心者です。 SwiftyCam(https://github.com/Awalz/SwiftyCam

+0

http://stackoverflow.com/a/34470541/4475605 – Adrian

+0

が重複する可能性に構文を変換することによって、それを使用することができます/stackoverflow.com/questions/34469189/how-to-switch-to-front-camera-in-swift-2) – shallowThought

答えて

0

私がswift 2で行ったこともあります。 /:あなたは[?スウィフト2にフロントカメラに切り替える方法](HTTPのSWIFT 3.

@IBAction func onFrontRearCameraClick(sender: AnyObject) 
{ 
    if self.captureDevice != nil 
    { 
     if captureSession.running 
     { 
      print("captureSession running") 
     } 

     if btnFrontRearCamera.selected 
     { 
      captureDevice = self.getDeviceWithPosition(AVCaptureDevicePosition.Back) 
     } 
     else 
     { 
      captureDevice = self.getDeviceWithPosition(AVCaptureDevicePosition.Front) 
     } 

     if captureSession.running 
     { 
      //Indicate that some changes will be made to the session 
      captureSession.beginConfiguration() 

      let currentInput : AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput 
      captureSession.removeInput(currentInput) 

      do { 
       try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice)) 
      } catch { 
       print("Error adding video input device") 
      } 

      //Commit all the configuration changes at once 
      captureSession.commitConfiguration() 

      return 
     } 
    } 
}