Created
January 28, 2020 09:22
-
-
Save TeslaCrest/e5006f040bd97ecf2d6d0ff9d214a96a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func prepare(completionHandler: @escaping (Error?) -> Void){ | |
func createCaptureSession(){ | |
self.captureSession = AVCaptureSession() | |
} | |
func configureCaptureDevices() throws { | |
let camera = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front) | |
self.frontCamera = camera | |
try camera?.lockForConfiguration() | |
camera?.unlockForConfiguration() | |
} | |
func configureDeviceInputs() throws { | |
guard let captureSession = self.captureSession else { throw CameraControllerError.captureSessionIsMissing } | |
if let frontCamera = self.frontCamera { | |
self.frontCameraInput = try AVCaptureDeviceInput(device: frontCamera) | |
if captureSession.canAddInput(self.frontCameraInput!) { captureSession.addInput(self.frontCameraInput!)} | |
else { throw CameraControllerError.inputsAreInvalid } | |
} | |
else { throw CameraControllerError.noCamerasAvailable } | |
captureSession.startRunning() | |
} | |
DispatchQueue(label: "prepare").async { | |
do { | |
createCaptureSession() | |
try configureCaptureDevices() | |
try configureDeviceInputs() | |
} | |
catch { | |
DispatchQueue.main.async{ | |
completionHandler(error) | |
} | |
return | |
} | |
DispatchQueue.main.async { | |
completionHandler(nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment