简体   繁体   English

如何使用AVFoundation和摄像机视图更改fps和隐藏状态栏

[英]How to change fps and hide status bar using AVFoundation and a camera view

I am working with AVFoundation and making a fullscreen camera view. 我正在使用AVFoundation并进行全屏摄像头视图。 I can't seem to be able to change the fps and hide the status bar. 我似乎无法更改fps并隐藏状态栏。 I would like the fps to be set at 140 fps (for the iPhone 7) and I would also like the status bar to be hidden (I have changed that in my storyboard files and in the General tab of the Xcode app settings. How can I achieve this? Thanks in advance! (I am using Swift 3.0 and would prefer an answer in Swift 3 (if possible)) 我希望将fps设置为140 fps(对于iPhone 7),并且还希望隐藏状态栏(我在情节提要文件和Xcode应用程序设置的“常规”选项卡中更改了状态栏。我实现了吗?在此先感谢!(我正在使用Swift 3.0,并且希望在Swift 3中提供答案(如果可能))

Code of ViewController: `class ViewController: UIViewController { ViewController的代码:`类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) To hide a status bar you should add an override of prefersStatusBarHidden: method of UIViewController : 1)要隐藏状态栏,您应该添加UIViewControllerprefersStatusBarHidden:方法的覆盖:

override var prefersStatusBarHidden : Bool {
    return true
}

2) To set a constant FPS you can use activeVideoMinFrameDuration and activeVideoMaxFrameDuration properties of AVCaptureDevice instance: 2)要设置恒定的FPS,可以使用AVCaptureDevice实例的activeVideoMinFrameDurationactiveVideoMaxFrameDuration属性:

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))")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM