简体   繁体   English

控制帧速率或限制AVCaptureVideoPreviewLayer

[英]Controlling the frame rate or throttling a AVCaptureVideoPreviewLayer

I'm using an AVCaptureVideoPreviewLayer to allow the user to frame a shot from the iPhone camera. 我正在使用AVCaptureVideoPreviewLayer来允许用户从iPhone相机拍摄一张照片。 So I have an AVCaptureSession with the input as an AVCaptureDeviceInput, and the output as an AVCaptureStillImageOutput. 所以我有一个AVCaptureSession,其输入为AVCaptureDeviceInput,输出为AVCaptureStillImageOutput。

I also have animations and controls on top of the video feed, but these are slow and jerky because the video behind is running at maximum frame rate and tying up the CPU/GPU. 我还在视频输入的顶部有动画和控件,但这些都很慢而且生涩,因为背后的视频以最大帧速率运行并且占用了CPU / GPU。

I'd like to cap the frame rate of the AVCaptureVideoPreviewLayer. 我想限制AVCaptureVideoPreviewLayer的帧速率。 I see there's the minFrameDuration property on AVCaptureVideoDataOutput, but I can't find anything similar on AVCaptureVideoPreviewLayer. 我看到AVCaptureVideoDataOutput上有minFrameDuration属性,但我在AVCaptureVideoPreviewLayer上找不到类似的东西。

I do not think that the problem is connected with the frame rate. 我不认为问题与帧速率有关。 So I will suggest some tips to improve performance of your app: 因此,我会建议一些提高应用性能的提示:

1) AVCaptureVideoPreviewLayer it is just a subclass of CALayer which displays output from camera, so it is impossible to cap the frame rate of it. 1)AVCaptureVideoPreviewLayer它只是CALayer的子类,它显示来自摄像头的输出,因此不可能限制它的帧速率。

2) Check if you positioned your animations in the right place, it depends on what kind of animations you have, if it is CALayer then animation layer should be a sublayer of your main canvas view layer(NOT AVCaptureVideoPreviewLayer!!!), if it is UIView, then it must be a subview of your main canvas view. 2)检查你的动画是否放在正确的位置,这取决于你有什么样的动画,如果它是CALayer,那么动画层应该是你的主画布视图层的子层(不是AVCaptureVideoPreviewLayer !!!),如果它是UIView,那么它必须是主画布视图的子视图。

3) You can improve performance of your app by setting session preset: 3)您可以通过设置会话预设来提高应用的性能:

[captureSession setSessionPreset:AVCaptureSessionPresetLow];

By default it is set to high, you may set it whatever you need, it is just a quality of video and if it is high performance could not be ideal. 默认情况下,它设置为高,您可以根据需要设置它,它只是一个视频质量,如果它是高性能可能不是理想的。

4) I made my own test app, where is random animation overlays video preview layer(but it is subview of my main view!!!) and everything went smooth even on my old iPod, I can give you a code for initialization of capture session: 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) And finally, in iOS5 you can set min and max video frame rate, what also can improve performance of your app, I guess it is what you asked. 5)最后,在iOS5中你可以设置最小和最大视频帧率,还可以提高你的应用程序的性能,我想这就是你的要求。 Check this link(Setting minimum and maximum video frame rate): 检查此链接(设置最小和最大视频帧速率):

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

Hope that my answer was clear. 希望我的回答很清楚。

Best wishes, 最好的祝愿,

Artem 阿尔乔姆

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

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