如何使用 AVFoundation 和相机视图更改 fps 和隐藏状态栏
How to change fps and hide status bar using AVFoundation and a camera view
我正在使用 AVFoundation 制作全屏相机视图。我似乎无法更改 fps 并隐藏状态栏。我希望将 fps 设置为 140 fps(对于 iPhone 7)并且我还希望隐藏状态栏(我已经在故事板文件和 Xcode 应用程序设置。我怎样才能做到这一点?提前致谢!(我正在使用 Swift 3.0,希望在 Swift 3(如果可能)中得到答案)
ViewController 代码:`class ViewController: UIViewController {
@IBOutlet var cameraView: UIImageView!
let captureSession = AVCaptureSession()
let stillImageOutput = AVCaptureStillImageOutput()
var previewLayer : AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice?
func beginSession() {
do {
try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if captureSession.canAddOutput(stillImageOutput) {
captureSession.addOutput(stillImageOutput)
}
}
catch {
print("error: \(error.localizedDescription)")
}
guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else {
print("no preview layer")
return
}
self.view.layer.addSublayer(previewLayer)
previewLayer.frame = self.view.layer.frame
captureSession.startRunning()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
captureSession.sessionPreset = AVCaptureSessionPresetHigh
if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] {
// Loop through all the capture devices on this phone
for device in devices {
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera
if(device.position == AVCaptureDevicePosition.back) {
captureDevice = device
if captureDevice != nil {
print("Capture device found")
beginSession()
}
}
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
`
1) 要隐藏状态栏,您应该添加对 UIViewController
的 prefersStatusBarHidden:
方法的覆盖:
override var prefersStatusBarHidden : Bool {
return true
}
2) 要设置恒定 FPS,您可以使用 AVCaptureDevice
实例的 activeVideoMinFrameDuration
and activeVideoMaxFrameDuration
属性:
func setFrameRate(_ captureDevice: AVCaptureDevice) {
do {
try captureDevice.lockForConfiguration()
captureDevice.activeVideoMinFrameDuration = CMTimeMake(1, 140)
captureDevice.activeVideoMaxFrameDuration = CMTimeMake(1, 140)
captureDevice.unlockForConfiguration()
} catch {
NSLog("An Error occurred: \(error.localizedDescription))")
}
}
我正在使用 AVFoundation 制作全屏相机视图。我似乎无法更改 fps 并隐藏状态栏。我希望将 fps 设置为 140 fps(对于 iPhone 7)并且我还希望隐藏状态栏(我已经在故事板文件和 Xcode 应用程序设置。我怎样才能做到这一点?提前致谢!(我正在使用 Swift 3.0,希望在 Swift 3(如果可能)中得到答案)
ViewController 代码:`class ViewController: UIViewController {
@IBOutlet var cameraView: UIImageView!
let captureSession = AVCaptureSession()
let stillImageOutput = AVCaptureStillImageOutput()
var previewLayer : AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice?
func beginSession() {
do {
try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
if captureSession.canAddOutput(stillImageOutput) {
captureSession.addOutput(stillImageOutput)
}
}
catch {
print("error: \(error.localizedDescription)")
}
guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else {
print("no preview layer")
return
}
self.view.layer.addSublayer(previewLayer)
previewLayer.frame = self.view.layer.frame
captureSession.startRunning()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
captureSession.sessionPreset = AVCaptureSessionPresetHigh
if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] {
// Loop through all the capture devices on this phone
for device in devices {
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera
if(device.position == AVCaptureDevicePosition.back) {
captureDevice = device
if captureDevice != nil {
print("Capture device found")
beginSession()
}
}
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
`
1) 要隐藏状态栏,您应该添加对 UIViewController
的 prefersStatusBarHidden:
方法的覆盖:
override var prefersStatusBarHidden : Bool {
return true
}
2) 要设置恒定 FPS,您可以使用 AVCaptureDevice
实例的 activeVideoMinFrameDuration
and activeVideoMaxFrameDuration
属性:
func setFrameRate(_ captureDevice: AVCaptureDevice) {
do {
try captureDevice.lockForConfiguration()
captureDevice.activeVideoMinFrameDuration = CMTimeMake(1, 140)
captureDevice.activeVideoMaxFrameDuration = CMTimeMake(1, 140)
captureDevice.unlockForConfiguration()
} catch {
NSLog("An Error occurred: \(error.localizedDescription))")
}
}