Ios 将“切换摄影机”按钮添加到自定义摄影机视图
我从GitHub获得了这段代码,作为最基本的自定义相机视图控制器。我已经实现了一个闪光灯,如果我测试前后摄像头,它就会工作。但我如何实现一个按钮,让你在应用程序中切换相机?我已经创建了一个摄像头按钮按下动作,当点击摄像头开关按钮时,该动作将运行Ios 将“切换摄影机”按钮添加到自定义摄影机视图,ios,swift,camera,avfoundation,Ios,Swift,Camera,Avfoundation,我从GitHub获得了这段代码,作为最基本的自定义相机视图控制器。我已经实现了一个闪光灯,如果我测试前后摄像头,它就会工作。但我如何实现一个按钮,让你在应用程序中切换相机?我已经创建了一个摄像头按钮按下动作,当点击摄像头开关按钮时,该动作将运行 class CameraViewController: UIViewController { @IBOutlet weak var flashButton: UIButton! @IBOutlet weak var cameraButton: UIBu
class CameraViewController: UIViewController {
@IBOutlet weak var flashButton: UIButton!
@IBOutlet weak var cameraButton: UIButton!
var flashOn = false
@IBOutlet weak var previewView: PreviewView!
let captureSession = AVCaptureSession()
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
let capturePhotoOutput = AVCapturePhotoOutput()
let capturePhotoDelegate = CapturePhotoDelegate()
private var deviceInput: AVCaptureDeviceInput?
private var cameraPosition: CameraPosition = .back
enum CameraPosition {
case front
case back
}
override func viewDidLoad() {
super.viewDidLoad()
checkCameraUsagePermission()
flashButton.setTitle("OFF", for: .normal)
cameraButton.setTitle("BACK", for: .normal)
}
func initialiseCaptureSession() {
let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .unspecified)
guard let input = try? AVCaptureDeviceInput(device: captureDevice!),
captureSession.canAddInput(input)
else { return }
captureSession.addInput(input)
self.previewView.videoPreviewLayer.session = self.captureSession
self.previewView.videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
capturePhotoOutput.isHighResolutionCaptureEnabled = true
captureSession.addOutput(capturePhotoOutput)
captureSession.startRunning()
}
@IBAction func onTapTakePhoto(_ sender: UIButton) {
let photoSettings = AVCapturePhotoSettings()
photoSettings.isAutoStillImageStabilizationEnabled = true
photoSettings.isHighResolutionPhotoEnabled = true
photoSettings.flashMode = .auto
if flashOn == true {
photoSettings.flashMode = .on
} else if flashOn == false {
photoSettings.flashMode = .off
}
capturePhotoOutput.capturePhoto(with: photoSettings, delegate: capturePhotoDelegate)
}
func checkCameraUsagePermission() {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized:
self.initialiseCaptureSession()
case .notDetermined:
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
self.initialiseCaptureSession()
}
}
case .denied:
return
case .restricted:
return
}
}
@IBAction func flashButtonPressed(_ sender: UIButton) {
if flashOn == false {
flashOn = true
flashButton.setTitle("ON", for: .normal)
} else {
flashOn = false
flashButton.setTitle("OFF", for: .normal)
}
}
func addVideoInput(position: AVCaptureDevice.Position) {
guard let device: AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
for: .video, position: position) else { return }
if let currentInput = self.deviceInput {
self.captureSession.removeInput(currentInput)
self.deviceInput = nil
}
do {
let input = try AVCaptureDeviceInput(device: device)
if self.captureSession.canAddInput(input) {
self.captureSession.addInput(input)
self.deviceInput = input
}
} catch {
print(error)
}
}
@IBAction func cameraButtonPressed(_ sender: UIButton) {
switch self.cameraPosition {
case .front:
self.cameraPosition = .back
self.addVideoInput(position: .back)
case .back:
self.cameraPosition = .front
self.addVideoInput(position: .front)
}
//configure your session here
DispatchQueue.main.async {
self.captureSession.beginConfiguration()
if self.captureSession.canAddOutput(self.capturePhotoOutput) {
self.captureSession.addOutput(self.capturePhotoOutput)
}
self.captureSession.commitConfiguration()
}
}
}首先,将您的
AVCaptureDeviceInput
存储在属性中
专用var设备输入:AVCaptureDeviceInput?
下一步创建枚举,该枚举将指示哪个摄影机处于活动状态
enum CameraPosition {
case front
case back
}
和当前相机模式的属性
private var cameraPosition: CameraPosition = .back
现在创建负责摄像机切换的功能
func addVideoInput(position: AVCaptureDevice.Position) {
guard let device: AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
for: .video, position: position) else { return }
if let currentInput = self.deviceInput {
self.captureSession.removeInput(currentInput)
self.deviceInput = nil
}
do {
let input = try AVCaptureDeviceInput(device: device)
if self.captureSession.canAddInput(input) {
self.captureSession.addInput(input)
self.deviceInput = input
}
} catch {
print(error)
}
}
现在,在camerarbutton按下的方法中,您可以切换摄像机
@IBAction func cameraButtonPressed(_ sender: UIButton) {
switch self.cameraPosition {
case .front:
self.cameraPosition = .back
self.addVideoInput(position: .back)
case .back:
self.cameraPosition = .front
self.addVideoInput(position: .front)
}
//configure your session here
DispatchQueue.main.async {
self.captureSession.beginConfiguration()
if self.captureSession.canAddOutput(self.capturePhotoOutput) {
self.captureSession.addOutput(self.capturePhotoOutput)
}
self.captureSession.commitConfiguration()
}
}
非常感谢你的回答。我对编程非常陌生,一定没有正确地实现您的代码,因为我仍然无法让相机翻转。我应该简单地添加您的代码还是用它替换我的一些代码?对不起,只需添加我的代码。有关更多信息,请参阅我的GitHub repo。应用程序没有崩溃,但相机没有切换。您是否存储了设备输入?请提供您的更新代码什么是存储设备输入?