【问题标题】:Controlling the frame rate or throttling a AVCaptureVideoPreviewLayer控制帧速率或限制 AVCaptureVideoPreviewLayer
【发布时间】:2011-09-23 13:01:45
【问题描述】:

我正在使用 AVCaptureVideoPreviewLayer 来允许用户从 iPhone 相机构图。所以我有一个 AVCaptureSession,输入为 AVCaptureDeviceInput,输出为 AVCaptureStillImageOutput。

我在视频源上也有动画和控件,但这些动画和控件很慢而且很不稳定,因为后面的视频以最大帧速率运行并且占用了 CPU/GPU。

我想限制 AVCaptureVideoPreviewLayer 的帧速率。我看到 AVCaptureVideoDataOutput 上有 minFrameDuration 属性,但我在 AVCaptureVideoPreviewLayer 上找不到类似的东西。

【问题讨论】:

    标签: iphone ios cocoa-touch


    【解决方案1】:

    我认为问题与帧速率无关。因此,我将建议一些技巧来提高您的应用程序的性能:

    1) AVCaptureVideoPreviewLayer 它只是 CALayer 的一个子类,它显示来自相机的输出,所以不可能限制它的帧率。

    2) 检查您是否将动画放置在正确的位置,这取决于您拥有什么样的动画,如果是 CALayer,那么动画层应该是您的主画布视图层的子层(不是 AVCaptureVideoPreviewLayer !!!) ,如果是 UIView,那么它一定是你的主画布视图的子视图。

    3) 您可以通过设置会话预设来提高应用的性能:

    [captureSession setSessionPreset:AVCaptureSessionPresetLow];
    

    默认设置为高,你可以设置任何你需要的,它只是一个视频质量,如果它是高性能的就不是理想的了。

    4)我制作了自己的测试应用程序,随机动画覆盖视频预览层在哪里(但它是我主视图的子视图!!!)即使在我的旧 iPod 上一切都很顺利,我可以给你一个代码捕获会话的初始化:

    // Create a capture session
    self.captureSession = [AVCaptureSession new];
    if([captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]){
        [captureSession setSessionPreset:AVCaptureSessionPresetHigh];
    }
    else{
        // HANDLE ERROR
    }
    
    // Find a suitable capture device
    AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Create and add a device input
    NSError *error = nil;
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&error];
    if([captureSession canAddInput:videoInput]){
        [captureSession addInput:videoInput];
    }
    else{
        // HANDLE ERROR
    }
    
    // Create and add a device still image output
    AVCaptureStillImageOutput *stillImageOutput = [AVCaptureStillImageOutput new];
    [stillImageOutput addObserver:self forKeyPath:@"capturingStillImage" options:NSKeyValueObservingOptionNew context:AVCaptureStillImageIsCapturingStillImageContext];
    if([captureSession canAddOutput:stillImageOutput]){
        [captureSession addOutput:stillImageOutput];
    }
    else{
        // HANDLE ERROR
    }
    
    // Setting up the preview layer for the camera
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    previewLayer.frame = self.view.bounds;
    
    // ADDING FINAL VIEW layer TO THE CAMERA CANVAS VIEW sublayer
    [self.view.layer addSublayer:previewLayer];
    
    // start the session
    [captureSession startRunning];
    

    5) 最后,在 iOS5 中,您可以设置最小和最大视频帧速率,这也可以提高您的应用程序的性能,我想这就是您所要求的。检查此链接(设置最小和最大视频帧率):

    http://developer.apple.com/library/mac/#releasenotes/AudioVideo/RN-AVFoundation/_index.html

    希望我的回答很清楚。

    祝你好运,

    阿尔乔姆

    【讨论】:

    • 非常感谢您的回答,这是一个老问题,但 iOS 5 新增功能的消息非常有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    相关资源
    最近更新 更多