ios - 尝试使用 AV Foundation 捕获静止图像时不断出现 "inactive/invalid connection passed"错误

标签 ios iphone objective-c avfoundation avcapturesession

我有一个使用 AV Foundation 的 View Controller 。一旦加载了 View Controller ,用户就能够准确地看到输入设备所看到的内容。这是因为我在 viewDidLoad 方法实现中启动了 AVCaptureSession。

这是我在 viewDidLoad 中的代码:

[super viewDidLoad];


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


[session setSessionPreset:AVCaptureSessionPresetHigh];


AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


NSError *error = [[NSError alloc]init];


AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];


if([session canAddInput:deviceInput])
    [session addInput:deviceInput];


AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session];



[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];



CALayer *rootLayer = [[self view]layer];


[rootLayer setMasksToBounds:YES];



[previewLayer setFrame:CGRectMake(0, 0, rootLayer.bounds.size.width, rootLayer.bounds.size.height/2)];


[rootLayer insertSublayer:previewLayer atIndex:0];


[session startRunning];

然后我有一个 IBAction 方法实现,它已连接到此 View Controller 的 UIButton。这是 IBAction 实现的代码:

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc]init];


AVCaptureConnection *connection = [[AVCaptureConnection alloc]init];

[stillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

    NSLog(@"Image Data Captured: %@", imageDataSampleBuffer);
    NSLog(@"Any errors? %@", error);

    if(imageDataSampleBuffer) {


        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];


        UIImage *image = [[UIImage alloc]initWithData:imageData];


        NSLog(@"%@", image);


    }

}];

当我在我的 iPhone 上运行该应用程序并按下连接到此实现的按钮时,我在控制台中收到此错误:

*** -[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - inactive/invalid connection passed.'

我查看了 xcode 文档,它确实说“您只能使用 addConnection 将 AVCaptureConnection 实例添加到 session 中:如果 canAddConnection:返回 YES”,但我尝试对我的 AVCaptureSession 对象进行 addConnection 和 canAddConnection 的方法调用但它们甚至没有显示为可用选项。

我还在其他地方读到,对于 iOS,您不必手动创建连接,但这对我来说没有意义,因为在我的 IBAction 代码中有一个方法调用: captureStillImageAsynchronouslyFromConnection: 需要输入。

如果连接是自动为您创建的,它叫什么以便我可以将其用于输入?

这是我第一次与 AV Foundation 合作,我似乎无法弄清楚这个连接错误。

非常感谢任何帮助。

最佳答案

设置相机时,将 stillImageOutput 添加到 AVCaptureSession。

self.stillImageOutput = AVCaptureStillImageOutput()
let stillSettings = [AVVideoCodecJPEG:AVVideoCodecKey]
self.stillImageOutput.outputSettings = stillSettings
if(self.session.canAddOutput(self.stillImageOutput)){
self.session.addOutput(self.stillImageOutput)
}

然后拍照时,从 stillImageOutput 中获取 AVCaptureSession。

func takePhoto(sender: UIButton!){
let connection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo)

if(connection.enabled){
    self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(connection, completionHandler: {(buffer: CMSampleBuffer!, error: NSError!) -> Void in
        println("picture taken")//this never gets executed
    })
}

}

关于ios - 尝试使用 AV Foundation 捕获静止图像时不断出现 "inactive/invalid connection passed"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896212/

相关文章:

iphone - 带有语法高亮的 UITextView

ios - 核心数据发送和接收对象

ios - 无法加密我的 Realm 数据库

iphone - 如何自定义UITabBarController的 'More'按钮?

iphone - 如何在我的 iPhone 上运行的应用程序中浏览核心数据?

iphone - 如何在表格 View 单元格(每行)中添加文本字段并将标签设置为每个文本字段以访问它的文本

objective-c - 在 C/Objective-C 中将返回值放在括号中的目的是什么?

ios - 从多个 URL 解析 JSON 后显示 View

ios - 如何将 UIButton 添加到将与其单元格一起滚动的 UICollectionView?

java - 如何创建简单的网络服务