带有ARC的iOS5上的modalviewcontroller中的AVCaptureSession

pir*_*800 5 modalviewcontroller ios avcapturesession automatic-ref-counting

我疯狂地想要在我的项目中呈现和解散AVCaptureSession(在视图控制器中).我目前在iOS5.1上并启用了ARC.

我第一次出现viewcontroller并开始会话时可以正常工作但是当我解雇并再次出现时会话将无法启动.我订阅了"AVCaptureSessionRuntimeErrorNotification"通知并收到以下错误:

"错误域= AVFoundationErrorDomain代码= -11819"无法完成操作"UserInfo = 0x1a4020 {NSLocalizedRecoverySuggestion =稍后再试.,NSLocalizedDescription =无法完成操作}"

我假设在我的会话中没有正确发布某些内容,但是ARC没有发布,而是我将所有内容都设置为nil.

我的viewDidLoad方法基本上只是触发initCamera

initCamera方法:

AVCaptureSession *tmpSession = [[AVCaptureSession alloc] init];

session = tmpSession;
session.sessionPreset = AVCaptureSessionPresetMedium;
captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
rearCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
input = [AVCaptureDeviceInput deviceInputWithDevice:rearCamera error:&error];

if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}

[session addInput:input];

videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey, nil];
[videoDataOutput setVideoSettings:outputSettings];
[videoDataOutput setAlwaysDiscardsLateVideoFrames:YES];


queue = dispatch_queue_create("cameraQueue", DISPATCH_QUEUE_SERIAL);
[videoDataOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);

[session addOutput:videoDataOutput];

NSNotificationCenter *notify =
[NSNotificationCenter defaultCenter];

[notify addObserver: self
           selector: @selector(onVideoError:)
               name: AVCaptureSessionRuntimeErrorNotification
             object: session];


[session startRunning];


[rearCamera lockForConfiguration:nil];
rearCamera.whiteBalanceMode = AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance;
rearCamera.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
rearCamera.focusMode = AVCaptureFocusModeContinuousAutoFocus;
[rearCamera unlockForConfiguration];
Run Code Online (Sandbox Code Playgroud)

方法

captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)连接

在我第一次呈现模态视图控制器时被调用没有问题,但在第二次尝试时此方法停止被调用(因为会话未启动)

为了清理我在解除之前从我的父视图控制器调用stopSession,并执行以下操作:

if ([session isRunning]) {
    [session removeInput:input];
    [session stopRunning];

    [vImagePreview removeFromSuperview];
    vImagePreview = nil;


    input = nil;
    videoDataOutput = nil;
    captureVideoPreviewLayer = nil;
    session = nil;

    queue = nil;


}
Run Code Online (Sandbox Code Playgroud)

我觉得我已经尝试了各种各样的事情,例如在队列上执行dispatch_sync(队列,^ {})等待它被刷新,但这似乎没有什么区别(当调用dispatch_sync时在我的init camera方法中删除了dispatch_release调用.我也尝试过使用另一个问题中建议的dispatch_set_finalizer_f(queue,capture_cleanup)方法,但我不知道在capture_cleanup方法中需要实际进行什么,因为我找到的所有例子都是非ARC代码,他们在那里调用release指向自我.我还梳理了可以从Apple(SquareCam和AVCam)找到的所有示例代码,但这些代码也是非ARC的.任何帮助将不胜感激.

pir*_*800 5

我意识到我在后置摄像头上执行了setFocusPointOfInterest,出于某种原因,它在重新启动时破坏了会话.我不明白为什么会引起这个问题,但我会调查一下.