我制作了一个具有捕获和保存视频功能的应用程序。我为此使用了 AVFoundation,Apple 的 AVCam一直是我的指南。
我希望我能说清楚:
在我第一次发布处理 AVCamCaptureManager 的 videoViewController 之前,一切正常(在 AVCam 中,这将是 AVCamViewController)。之后,当我再次创建它时,视频在相机切换后立即冻结。即使重新运行也无济于事,清理或设备重置也无济于事。(有时其中一件事会有所帮助,但这不是规则)。
当不需要节省内存时,我会释放 videoViewController。
切换摄像头的代码与AVCam中基本相同:
NSError *error;
AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition position = currentVideoInput.device.position;
if (position == AVCaptureDevicePositionBack)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:frontFacingCamera error:&error];
else if (position == AVCaptureDevicePositionFront)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error];
if (newVideoInput != nil) {
[session beginConfiguration];
[session removeInput:currentVideoInput];
if ([session canAddInput:newVideoInput]) {
[session addInput:newVideoInput];
[self setVideoInput:newVideoInput];
} else {
[session addInput:currentVideoInput];
}
[session commitConfiguration];
[newVideoInput release];
} else if (error) {
NSLog(@"%@",[error localizedDescription]);
}
我关闭 videoView 的代码
[self.videoViewController.view removeFromSuperview];
self.videoViewController = nil;
我目前的“解决方法”是简单地保留它,即使我不需要它。
有人可以解释为什么会发生这种情况以及如何解决它。
编辑:已解决
正如 W Dyson 在他的回复中指出的那样,我应该在释放我的 videoViewController 之前停止会话,如下所示:
[[[self.videoViewController captureManager] session] stopRunning];
[self.videoViewController.view removeFromSuperview];
self.videoViewController = nil;
谢谢戴森。