尝试使用AV Foundation捕获静止图像时,不断出现“无效/无效连接已通过”错误

时间:2014-02-20 01:24:36

标签: ios iphone objective-c avfoundation avcapturesession

我有一个使用AV Foundation的View Controller。一旦View控制器加载,用户就能够准确地看到输入设备正在看到的内容。这是因为我在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实例添加到会话中:if canAddConnection:返回YES”,但是我尝试在我的AVCaptureSession对象上为addConnection和canAddConnection进行方法调用但它们甚至没有显示为可用选项。

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

因此,如果为您自动创建连接,它的名称是什么,我可以将它用于输入?

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

设置相机时,请在AVCaptureSession中添加一个stillImageOutput。

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
    })
}

}

答案 1 :(得分:0)

您是否有保留AVCaptureSession的属性,例如

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

我希望它可以帮到你。