【问题标题】:Keep getting "inactive/invalid connection passed" error when trying to capture still image using AV Foundation尝试使用 AV Foundation 捕获静止图像时不断收到“inactive/invalid connection pass”错误
【发布时间】:2014-02-20 01:24:35
【问题描述】:

我有一个使用 AV Foundation 的视图控制器。视图控制器一加载,用户就可以准确地看到输入设备看到的内容。这是因为我已经在 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 方法实现,它已连接到此视图控制器的 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 实例添加到会话中:如果 canAddConnection:返回 YES”,但我已经尝试对我的 AVCaptureSession 对象执行 addConnection 和 canAddConnection 的方法调用但它们甚至没有显示为可用选项。

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

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

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

非常感谢任何帮助。

【问题讨论】:

    标签: ios iphone objective-c avfoundation avcapturesession


    【解决方案1】:

    当您设置相机时,将 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
        })
    }
    

    }

    【讨论】:

      【解决方案2】:

      你有没有保留 AVCaptureSession 的属性,比如

      @property (nonatomic, strong) AVCaptureSession *captureSession;
      //...
      self.captureSession = session;
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2014-03-20
        • 1970-01-01
        • 1970-01-01
        • 2019-05-15
        • 2010-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-01
        相关资源
        最近更新 更多