【问题标题】:AVCaptureVideoPreviewLayer running slower with timeAVCaptureVideoPreviewLayer 运行速度随时间变慢
【发布时间】:2013-12-03 22:35:28
【问题描述】:

我有一个 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];

我有什么问题吗?或者你有什么线索吗?

【问题讨论】:

  • 你是如何创建 VC 的?你有财产吗?你每次都创建一个新的 VC 吗?
  • 您在 VC 中时是否收到内存警告?如果打开一次会怎样?
  • 每次我需要拍照时,我都会创建一个 ViewController:CameraViewController *camera = [[CameraViewController alloc] initWithBlackboard:self.farm mode:3];
  • 我在 ViewController 打开时收到内存警告,如果我只打开一次 ViewController 一切正常
  • 您是否添加了一些对您的 VC 的引用?听起来 VC 没有被释放。

标签: ios ios6 uiviewcontroller avcapturesession


【解决方案1】:

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

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];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-20
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2021-05-02
    相关资源
    最近更新 更多