如何正确清理AVCaptureSession和AVCaptureVideoPreviewLayer

edd*_*ddy 7 ios4 ios

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

我发现这个问题的最佳答案是在这个SO线程中,谢谢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];     
}
Run Code Online (Sandbox Code Playgroud)

这里是capture_cleanup回调:

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

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是从stopCameraCapture中的超级层删除_previewLayer导致以下控制台错误:

"......修改正在最终确定的层......"

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

我不明白为什么我收到控制台错误以及如何解决它.[_previewLayer removeFromSuperlayer]如果没有调用self.dealloc,为什么在我调用时正在完成Layer .

注意:self是一个viewController,我还没有弹出它,所以它由NavigationContoller保留.

Hen*_*nry 3

尝试在释放之前停止会话:

[captureSession stopRunning];
Run Code Online (Sandbox Code Playgroud)