iOS7 AVCapture captureOutput永远不会被调用

时间:2014-09-10 03:24:19

标签: ios objective-c

请理解我无法在此处上传整个代码。

我有

@interface BcsProcessor : NSObject <AVCaptureMetadataOutputObjectsDelegate> {}

和BcsProcessor有setupCaptureSessioncaptureOutput方法。

- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection


- (NSString*)setUpCaptureSession {
    NSError* error = nil;

    AVCaptureSession* captureSession = [[[AVCaptureSession alloc] init] autorelease];
    self.captureSession = captureSession;

       AVCaptureDevice* __block device = nil;
    if (self.isFrontCamera) {

        NSArray* devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        [devices enumerateObjectsUsingBlock:^(AVCaptureDevice *obj, NSUInteger idx, BOOL *stop) {
            if (obj.position == AVCaptureDevicePositionFront) {
                device = obj;
            }
        }];
    } else {
        device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    }

    AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

    AVCaptureMetadataOutput* output = [[[AVCaptureMetadataOutput alloc] init] autorelease];
    output.metadataObjectTypes = output.availableMetadataObjectTypes

    dispatch_queue_t outputQueue = dispatch_queue_create("com.1337labz.featurebuild.metadata", 0);
    [output setMetadataObjectsDelegate:self queue:outputQueue];

    captureSession.sessionPreset = AVCaptureSessionPresetPhoto;

    if ([captureSession canAddInput:input]) {
        [captureSession addInput:input];
    }

    if ([captureSession canAddOutput:output]) {
        [captureSession addOutput:output];
    }

    // setup capture preview layer
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];

    // run on next event loop pass [captureSession startRunning]
    [captureSession performSelector:@selector(startRunning) withObject:nil afterDelay:0];

    return nil;
}

因此,上面的代码会设置会话并添加AVCaptureMetadataOutput。并且BcsProcessor应该接收捕获的元数据。但是我的captureOutput方法没有收到任何数据,也没有被调用。

我会感谢任何帮助或评论。

1 个答案:

答案 0 :(得分:3)

首先确保您的输入和输出正确添加到会话中。您可以登录captureSession.inputscaptureSession.outputs进行检查。

第二,确保正确设置output.metadataObjectTypes意味着availableMetadataObjectTypes的输出不为空。如果在添加输出之前调用它,我相信这将是空的。

最后我没有看到您将预览图层添加到视图图层 在使用会话初始化图层后尝试...

self.previewLayer.frame = self.view.layer.bounds;
[self.view.layer addSublayer:previewLayer];