简体   繁体   中英

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. I can't seem to be able to change the fps and hide the status bar. 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))

Code of 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) To hide a status bar you should add an override of prefersStatusBarHidden: method of UIViewController :

override var prefersStatusBarHidden : Bool {
    return true
}

2) To set a constant FPS you can use activeVideoMinFrameDuration and activeVideoMaxFrameDuration properties of AVCaptureDevice instance:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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