iOS7 AVCapture captureOutput 永远不会被调用

标签 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;
}

所以上面的代码设置 session 并添加AVCaptureMetadataOutput .和 BcsProcessor应该接收捕获的元数据。但是我的 captureOutput 方法没有接收到任何数据,或者被调用。

我将不胜感激任何帮助或评论。

最佳答案

首先确保您的输入和输出正确添加到 session 中。您可以通过登录 captureSession.inputs 进行检查和 captureSession.outputs .

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

最后我没有看到你将预览层添加到 View 层
在使用 session 初始化图层后尝试...

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

关于iOS7 AVCapture captureOutput 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25756833/

相关文章:

ios - 在 OS X El Capitan 上通过 cocoapods 安装 pod 时出错

objective-c - 我应该使用 merge=union 将 .pbxproj 文件与 git merge 吗?

iOS 7 CBCentral 标识符无法识别的选择器

ios - 如何使用自动布局缩放/增加 iphone 4、5 和 6 的字体大小

ios - 如何处理核心数据保存错误?

objective-c - Objective C - 如何使用外部变量?

ios - 如何多次调用 viewDidLoad

ios - 类 myClass 的实例 0xa9923e0 已被释放,而键值观察者仍向其注册

ios - 线程 1 : EXC_BAD_ACCESS (code=2, 地址=..) 使用 Macaw Pod iOS Swift?

android - 在 react native 应用程序中从 native 基础实现抽屉