如何使用AVCaptureSession流式传输实时预览视频,然后拍照,然后返回流式传输

时间:2010-12-10 01:47:26

标签: iphone avcapturesession

我有一个应用程序在拍摄静态照片之前创建自己的实时预览。应用程序需要对图像数据运行一些处理,因此无法依赖AVCaptureVideoPreviewLayer。使用Apple的示例代码,使初始流工作进展顺利。当我尝试切换到更高质量的图像拍摄快照时出现问题。响应按钮按下,我尝试重新配置会话以拍摄全分辨率照片。我尝试了很多变化,但这是我最新的例子(仍然不起作用):

- (void)sessionSetupForPhoto
{
 [session beginConfiguration];
 session.sessionPreset = AVCaptureSessionPresetPhoto;
 AVCaptureStillImageOutput *output = [[[AVCaptureStillImageOutput alloc] init] autorelease];
 for (AVCaptureOutput *output in [session outputs]) {
  [session removeOutput:output];
 }
 if ([session canAddOutput:output]){
  [session addOutput:output];
 } else {
  NSLog(@"Not able to add an AVCaptureStillImageOutput");
 }
 [session commitConfiguration];
}

我一直在commitConfiguration行之后收到一条错误消息,如下所示: (也就是说,我将AVCaptureSessionRuntimeErrorNotification发送给我的注册观察员)

  

收到错误:   NSConcreteNotification 0x19d870 {name   = AVCaptureSessionRuntimeErrorNotification;   object =;   userInfo = {       AVCaptureSessionErrorKey =“错误域= AVFoundationErrorDomain   代码= -11800 \“操作   无法完成。   (AVFoundationErrorDomain错误   -11800。)\“UserInfo = 0x19d810 {}”;

XCode中的文档表面上提供了有关错误编号(-11800)的更多信息,“AVErrorUnknown - 错误原因未知。”;

以前我也曾尝试过stopRunning和startRunning的调用,但在观看WWDC Session 409后不再这样做了,不鼓励这样做。当我停止并开始时,我得到一个不同的错误消息-11819,它对应于“AVErrorMediaServicesWereReset - 操作无法完成,因为媒体服务变得不可用。”,这比简单的“未知”要好得多,但不一定更有帮助。

它成功添加了AVCaptureStillImageOutput(即,不发出日志消息)。

我正在测试iPhone 3g(附4.1和iPhone 4)。

此调用发生在主线程中,这也是我原始AVCaptureSession设置发生的地方。

如何避免错误?如何切换到更高的分辨率拍摄照片?

谢谢!

2 个答案:

答案 0 :(得分:2)

由于您正在处理来自AVCaptureSession的视频数据,我假设您在调用 sessionSetupForPhoto之前已连接了AVCaptureVideoDataOutput。

如果是这样,你能详细说明你在 captureOutput:didOutputSampleBuffer:中做了什么吗?无法看到更多内容,我猜测删除旧输出并随后设置照片质量预设可能会出现问题。

此外,删除输出时用作迭代器的输出变量隐藏了静止图像输出。不是问题,但它使代码更难阅读。

答案 1 :(得分:1)

无需切换会话。只需在初始化时将AVCaptureStillImageOutput添加到您的会话中,并在您要捕获图像时调用以下内容并相应地使用CMSampleBufferRef

captureStillImageAsynchronouslyFromConnection:videoConnection
   completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) 
{
}
相关问题