ios - 如何使用 AVFoundation 和相机 View 更改 fps 并隐藏状态栏

标签 ios swift camera avfoundation frame-rate

我正在与 AVFoundation 合作并制作全屏相机 View 。我似乎无法更改 fps 并隐藏状态栏。我希望将 fps 设置为 140 fps(对于 iPhone 7),并且还希望隐藏状态栏(我已在 Storyboard文件和 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) 要隐藏状态栏,您应该添加 prefersStatusBarHidden: 的覆盖UIViewController的方法:

override var prefersStatusBarHidden : Bool {
    return true
}

2) 要设置恒定的 FPS,您可以使用 activeVideoMinFrameDurationactiveVideoMaxFrameDuration AVCaptureDevice 实例的属性:

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

关于ios - 如何使用 AVFoundation 和相机 View 更改 fps 并隐藏状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45449119/

相关文章:

ios - 是否可以在 AVCaptureVideoDataOutput 中设置 AVCaptureSessionPresetPhoto?

objective-c - 作为 UIBackgroundTask 运行时,后台线程上的核心数据迁移失败

ios - Xcode 8 SiriKit 扩展 - 无法附加到 pid

ios - Swift/CollectionViewWaterfallLayout/如何设置单元格数量

swift - 我的 scrollViewDidScroll 函数没有收到调用

ios - UISearchController 与单元格布局困惑

swift - 通过按下按钮更新 UIView

c++ - 使用 DMA 仅从 DCMI 读取每秒的字节

ios - iPhone 3GS 和 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange 格式?

android - 当我选择任何第三方相机应用程序时相机 Intent 中出现空指针异常