【发布时间】:2019-01-01 11:32:18
【问题描述】:
我已经使用 AVFoundation 实现了预览相机,它工作正常。但是我很难前后切换相机。我在底部栏添加了一个切换按钮。默认情况下,它的后置摄像头,我想将其切换到前置。我该怎么做?
class FifteenSecsViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {
@IBOutlet weak var camPreview: UIView!
let captureSession = AVCaptureSession()
let movieOutput = AVCaptureMovieFileOutput()
var previewLayer: AVCaptureVideoPreviewLayer!
var activeInput: AVCaptureDeviceInput!
var outputURL: URL!
override func viewDidLoad() {
super.viewDidLoad()
if setupSession() {
setupPreview()
startSession()
}
self.switchCameraButton.addTarget(self, action: #selector(switchButtonTapped), for: .touchUpInside)
}
func setupSession() -> Bool {
captureSession.sessionPreset = AVCaptureSession.Preset.high
// Setup Camera
let camera: AVCaptureDevice?
camera = AVCaptureDevice.default(for: .video)
do {
let input = try AVCaptureDeviceInput(device: camera!)
if captureSession.canAddInput(input) {
captureSession.addInput(input)
activeInput = input
}
} catch {
print("Error setting device video input: \(error)")
return false
}
// Setup Microphone
let microphone = AVCaptureDevice.default(for: .audio)
do {
let micInput = try AVCaptureDeviceInput(device: microphone!)
if captureSession.canAddInput(micInput) {
captureSession.addInput(micInput)
}
} catch {
print("Error setting device audio input: \(error)")
return false
}
// Movie output
let seconds : Int64 = 3
let maxDuration = CMTime(seconds: Double(seconds),
preferredTimescale: 1)
movieOutput.maxRecordedDuration = maxDuration
if captureSession.canAddOutput(movieOutput) {
captureSession.addOutput(movieOutput)
}
return true
}
func setupPreview() {
// Configure previewLayer
previewLayer = AVCaptureVideoPreviewLayer(session:
captureSession)
previewLayer.frame = camPreview.bounds
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
camPreview.layer.addSublayer(previewLayer)
}
//MARK:- Camera Session
func startSession() {
if !captureSession.isRunning {
videoQueue().async {
self.captureSession.startRunning()
}
}
}
@objc func switchButtonTapped(){
// what to write here??
}
}
函数switchButtonTapped 是UIButton 的actionTarget。如果我在此按钮中添加此代码:
@objc func switchButtonTapped(){
if setupSession() {
setupPreview()
startSession()
}
}
Camerapreview 屏幕显示白屏并卡住。
【问题讨论】:
-
查看此链接可能对您有帮助 stackoverflow.com/questions/20864372/…
-
不鼓励仅提供链接作为答案。如果页面移动,链接可能会中断。请考虑解释您提供的答案的基础知识,因为我们不是在这里进行 OP 可以自己进行的基本搜索。
标签: ios swift avfoundation