Created
October 24, 2020 18:34
-
-
Save rorodriguez116/f722af7f7aba72af75dd86a9e5355d6c to your computer and use it in GitHub Desktop.
Changes active camera between front and rear.
This file contains hidden or 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
// MARK: Device Configuration | |
/// - Tag: ChangeCamera | |
public func changeCamera() { | |
// MARK: Here disable all camera operation related buttons due to configuration is due upon and must not be interrupted | |
DispatchQueue.main.async { | |
self.isCameraButtonDisabled = true | |
} | |
// | |
sessionQueue.async { | |
let currentVideoDevice = self.videoDeviceInput.device | |
let currentPosition = currentVideoDevice.position | |
let preferredPosition: AVCaptureDevice.Position | |
let preferredDeviceType: AVCaptureDevice.DeviceType | |
switch currentPosition { | |
case .unspecified, .front: | |
preferredPosition = .back | |
preferredDeviceType = .builtInWideAngleCamera | |
case .back: | |
preferredPosition = .front | |
preferredDeviceType = .builtInWideAngleCamera | |
@unknown default: | |
print("Unknown capture position. Defaulting to back, dual-camera.") | |
preferredPosition = .back | |
preferredDeviceType = .builtInWideAngleCamera | |
} | |
let devices = self.videoDeviceDiscoverySession.devices | |
var newVideoDevice: AVCaptureDevice? = nil | |
// First, seek a device with both the preferred position and device type. Otherwise, seek a device with only the preferred position. | |
if let device = devices.first(where: { $0.position == preferredPosition && $0.deviceType == preferredDeviceType }) { | |
newVideoDevice = device | |
} else if let device = devices.first(where: { $0.position == preferredPosition }) { | |
newVideoDevice = device | |
} | |
if let videoDevice = newVideoDevice { | |
do { | |
let videoDeviceInput = try AVCaptureDeviceInput(device: videoDevice) | |
self.session.beginConfiguration() | |
// Remove the existing device input first, because AVCaptureSession doesn't support | |
// simultaneous use of the rear and front cameras. | |
self.session.removeInput(self.videoDeviceInput) | |
if self.session.canAddInput(videoDeviceInput) { | |
self.session.addInput(videoDeviceInput) | |
self.videoDeviceInput = videoDeviceInput | |
} else { | |
self.session.addInput(self.videoDeviceInput) | |
} | |
if let connection = self.photoOutput.connection(with: .video) { | |
if connection.isVideoStabilizationSupported { | |
connection.preferredVideoStabilizationMode = .auto | |
} | |
} | |
self.photoOutput.maxPhotoQualityPrioritization = .quality | |
self.session.commitConfiguration() | |
} catch { | |
print("Error occurred while creating video device input: \(error)") | |
} | |
} | |
DispatchQueue.main.async { | |
// MARK: Here enable capture button due to successfull setup | |
self.isCameraButtonDisabled = false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment