如何正确清理AVCaptureSession和AVCaptureVideoPreviewLayer

时间:2011-03-23 18:52:18

标签: ios ios4

我正在使用AVFoundation api创建相机预览视图,我在完成后无法清理。

我发现这个问题的最佳答案是SO thread,谢谢Codo。

但是,他没有解决AVCaptureVideoPreviewLayer的重新分配问题,而这正是我遇到麻烦的地方。

在我的视图控制器类中,我在startCameraCapture方法中有一些初始化代码。听取Codo的回答,我正在使用dispatch_set_finalizer_f(_captureQueue, capture_cleanup);注册一个回调,以便在队列真正关闭时调用。 我也保留自己,以确保在调用我的对象完成队列之前我的对象不会消失。然后我使用capture_cleanup回调来释放self。

-(void) startCameraCapture {
    _camSession = [[AVCaptureSession alloc] init];
    if (_previewLayer == nil) {
        _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_camSession];
    }
    _previewLayer.frame = self.compView.bgView.frame;   
    [self.compView.bgView.layer addSublayer:_previewLayer];

    // Get the default camera device
    AVCaptureDevice* camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // Create a AVCaptureInput with the camera device
    NSError *error=nil;
    AVCaptureInput* cameraInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];
    if (cameraInput == nil) {
        NSLog(@"Error to create camera capture:%@",error);

    }

    AVCaptureVideoDataOutput* videoOutput = [[[AVCaptureVideoDataOutput alloc] init] autorelease];

    // create a queue to run the capture on
    _captureQueue=dispatch_queue_create("captureQueue", NULL);
    dispatch_set_context(_captureQueue, self);
    dispatch_set_finalizer_f(_captureQueue, capture_cleanup);

    // setup our delegate
    [videoOutput setSampleBufferDelegate:self queue:_captureQueue];

    dispatch_release(_captureQueue);

    // retain self as a workouround a queue finalization bug in apples's sdk 
    // per Stackoverflow answer https://stackoverflow.com/questions/3741121/how-to-properly-release-an-avcapturesession
    [self retain];

    // configure the pixel format
    videoOutput.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey,
                                 nil];

    // and the size of the frames we want
    [_camSession setSessionPreset:AVCaptureSessionPresetMedium];

    // Add the input and output
    [_camSession addInput:cameraInput];
    [_camSession addOutput:videoOutput];

    [cameraInput release];

    // Start the session
    [_camSession startRunning];     
}

这里是capture_cleanup回调:

 static void capture_cleanup(void* p)
    {
        LiveCompViewController* ar = (LiveCompViewController*)p;
        [ar release];  // releases capture session if dealloc is called
    }

然后我的清理代码如下所示:

-(void) stopCameraCapture {
[_camSession stopRunning];
    [_camSession release];
    _camSession=nil;    

    // Remove the layer in order to release the camSession
    [_previewLayer removeFromSuperlayer];
    _previewLayer = nil;

}

我遇到的问题是在stopCameraCapture中从superlayer中删除_previewLayer会导致以下控制台错误:

  

“......修改正在最终确定的图层......”

但我需要删除该层,以便释放并释放它,以便释放_camSession,然后释放dispatch_queue,然后最终调用我的capture_cleanup回调,最终释放self。

我不明白为什么我收到控制台错误以及如何修复它。如果未调用self.dealloc,为什么在我调用[_previewLayer removeFromSuperlayer]时最终确定Layer。

注意:self是一个viewController,我还没有弹出它,因此它被NavigationContoller保留。

1 个答案:

答案 0 :(得分:3)

尝试在发布之前停止会话:

[captureSession stopRunning];
相关问题