AVCaptureMetadataOutput setMetadataObjectTypes:找不到支持的类型

时间:2014-08-25 07:07:51

标签: ios7 qr-code

我使用ios7 API扫描QRcode,代码如下:

    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    [self.captureSession addOutput:output];
    [output setMetadataObjectsDelegate:self queue:captureOutputBufferQueue];
    output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];

我收到Crashlytics的崩溃信息:

  NSInvalidArgumentException
  *** -[AVCaptureMetadataOutput setMetadataObjectTypes:] - unsupported type found. Use -     availableMetadataObjectTypes.
 Thread : Fatal Exception: NSInvalidArgumentException
 0  CoreFoundation                 0x303b9f83 __exceptionPreprocess + 130
 1  libobjc.A.dylib                0x3ab6accf objc_exception_throw + 38
 2  AVFoundation                   0x2f2f7c29 -[AVCaptureMetadataOutput rectOfInterest]

有人见过这个吗?

1 个答案:

答案 0 :(得分:2)

在设置metadataObjectTypes之前,必须将输出添加到会话。 我遇到了错误,并使用以下代码修复它。

@property (nonatomic, strong) AVCaptureMetadataOutput *output;
@property (nonatomic, strong) AVCaptureSession *session; 

- (AVCaptureMetadataOutput *)output
{
   if (!_output) {
       _output = [[AVCaptureMetadataOutput alloc] init];
       [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
   }
   return _output;
}

- (AVCaptureSession *)session
{
  if (!_session) {
       _session = [[AVCaptureSession alloc] init];

       [_session setSessionPreset:AVCaptureSessionPresetHigh];

       if ([_session canAddInput:self.input]) {
           [_session addInput:self.input];
       }

       if ([_session canAddOutput:self.output]) {
           [_session addOutput:self.output];

           NSArray *typeList = self.output.availableMetadataObjectTypes;
           NSLog(@"availableMetadataObjectTypes : %@", typeList);
           self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
       }
   }
   return _session;
}