AVCaptureVideoPreviewLayer随着时间的推移运行速度变慢

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AVCaptureVideoPreviewLayer随着时间的推移运行速度变慢相关的知识,希望对你有一定的参考价值。

我有一个UIViewController,在其中我在init方法上设置了一个AVCaptureVideoPreviewLayer来拍照。拍摄照片后,我关闭了UIViewController。

每次打开此UIViewController时,Ipad的运行速度都会变慢,直到崩溃为止。我收到内存警告,因此我认为它不是免费的,另一方面,我正在使用ARC,因此我支持每次我关闭UIViewController都会释放内存。

这是UIViewController的init方法上的代码:

    session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetMedium;

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    captureVideoPreviewLayer.affineTransform = CGAffineTransformMakeRotation(M_PI+M_PI_2);
    captureVideoPreviewLayer.frame = CGRectMake(45, 55, 512, 387);
    [self.cameraPlace.layer addSublayer:captureVideoPreviewLayer];


    NSArray *cameras=[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *device = [cameras objectAtIndex:1];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    [session addInput:input];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:stillImageOutput];

    [session startRunning];

我有什么问题吗?还是您有任何线索?

答案

我遇到了同样的问题,因为我正在添加带有已添加会话的层。

self.captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer 
                layerWithSession:self.session];

请在上面的行上设置断点,您会发现需要3-4秒才能继续。有时会创建内存警告,有时则不会。因此只需添加一下,检查它是否已经添加,如下所示。

NSError *error;
[self.deviceCamera lockForConfiguration:&error];

if (self.session == nil)
{
    self.session = [[AVCaptureSession alloc] init];  
    self.captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer 
    layerWithSession:self.session];
    CGRect screenBounds = [[UIScreen mainScreen]bounds];
    self.captureVideoPreviewLayer.frame = screenBounds;
    self.captureVideoPreviewLayer.videoGravity = 
    AVLayerVideoGravityResizeAspectFill; //  to fill the camera fullscreen view
    self.userInteractionEnabled = YES;

    if ([self.session canAddInput:_input]) {
        [self.session addInput:_input];
     }
}

[self.session startRunning];

[self.layer addSublayer:self.captureVideoPreviewLayer];

if (self.stillImageOutput ==  nil)
{
    self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [self.stillImageOutput setOutputSettings:outputSettings];
    NSString* preset = 0;
    if (!preset) {
        preset = AVCaptureSessionPresetPhoto;
    }

    if ([self.session canSetSessionPreset:preset]) {

        self.session.sessionPreset = preset;
    }


    if ([self.session canAddOutput:self.stillImageOutput]) {
        [self.session addOutput:self.stillImageOutput];
    }
}

以上是关于AVCaptureVideoPreviewLayer随着时间的推移运行速度变慢的主要内容,如果未能解决你的问题,请参考以下文章