AVFoundation 我使用 setActiveVideoMinFrameDuration 没有工作

AVFoundation I use setActiveVideoMinFrameDuration didn't work

我在演示中使用 AVCaptureVideoDataOutput,用于无声循环拍照(如扫描仪), 所以我将 fps 设置为低 Level

[device setActiveVideoMinFrameDuration:CMTimeMake(1, 1)];
[device setActiveVideoMaxFrameDuration:CMTimeMake(1, 1)];

在我的代码中,然后这样做

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
   fromConnection:(AVCaptureConnection *)connection
{
     NSLog(@"date");
}

检查它是否有效,我发现它每秒打印 24 次,而不是 1 次 1 秒

PS:设备版本为iPhone5C和iOS8.12

刚遇到一样problem.You应该看看setActiveVideoMinFrameDuration或setActiveVideoMaxFrameDuration的函数说明。 苹果说:

On iOS, the receiver's activeVideoMinFrameDuration resets to its default value under the following conditions:
- The receiver's activeFormat changes
- The receiver's AVCaptureDeviceInput's session's sessionPreset changes
- The receiver's AVCaptureDeviceInput is added to a session

因此,您应该在 更改 activeFormat、sessionPreset 和 AVCaptureSession 的 addInput 之后调用 setActiveVideoMinFrameDuration 和 setActiveVideoMaxFrameDuration

SWIFT

对于那些正在寻找优雅的 Swifty 解决方案的人,这是我从最新的官方文档中得到的

The following code example illustrates how to select an iOS device’s highest possible frame rate:

func configureCameraForHighestFrameRate(device: AVCaptureDevice) {

 var bestFormat: AVCaptureDevice.Format?
 var bestFrameRateRange: AVFrameRateRange?

 for format in device.formats {
     for range in format.videoSupportedFrameRateRanges {
         if range.maxFrameRate > bestFrameRateRange?.maxFrameRate ?? 0 {
             bestFormat = format
             bestFrameRateRange = range
         }
     }
 }

 if let bestFormat = bestFormat, 
    let bestFrameRateRange = bestFrameRateRange {
     do {
         try device.lockForConfiguration()

         // Set the device's active format.
         device.activeFormat = bestFormat

         // Set the device's min/max frame duration.
         let duration = bestFrameRateRange.minFrameDuration
         device.activeVideoMinFrameDuration = duration
         device.activeVideoMaxFrameDuration = duration

         device.unlockForConfiguration()
     } catch {
         // Handle error.
     }
 }
}

参考: Apple Official Documentation