2014-10-07 134 views
4

我正在嘗試使用AVFoundation以下面的代碼讀取條形碼,但我不斷收到下面的錯誤。幫助爲什麼將不勝感激。提前致謝!AVFoundation元數據對象類型

//Create camera view 
     session = AVCaptureSession() 
     var layer = self.cameraView.layer 
     vidLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as AVCaptureVideoPreviewLayer 
     vidLayer.frame = self.cameraView.bounds 
     vidLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
     var device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 
     var error:NSError? = nil 
     var input:AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &error) as? AVCaptureDeviceInput 
     var output = AVCaptureMetadataOutput(); 
     session.addOutput(output) 
     output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) 
     output.metadataObjectTypes = [AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode] 
     if(input != nil){ 
      println("ADDED") 
      session.addInput(input) 
     } 
     layer.addSublayer(vidLayer) 
     session.startRunning() 

2014-10-07 15:25:09.279 WegmannMobile[457:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] - unsupported type found. Use -availableMetadataObjectTypes.' 
*** First throw call stack: 
(0x2d7edf83 0x38068ccf 0x2c72bc29 0x618f0 0x62060 0x300256df 0x3019b43f 0x300b8d63 0x300b8b6d 0x300b8b05 0x3000ad59 0x2fc8862b 0x2fc83e3b 0x2fc83ccd 0x2fc836df 0x2fc834ef 0x2fc7d21d 0x2d7b9255 0x2d7b6bf9 0x2d7b6f3b 0x2d721ebf 0x2d721ca3 0x3267b663 0x3006e14d 0x9eff4 0x9f030 0x38575ab7) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

回答

3

這是其中錯誤消息非常有用的罕見情況之一。

致電[output availableMetadataObjectTypes]並確保您要設置的內容在那裏。如果不是,則不要嘗試設置該值。

+1

該數組完全是空的。這是爲什麼? – steventnorris 2014-10-07 20:34:33

+0

@steventnorris我想你在模擬器中調試,請嘗試在一個設備上。 – 2015-04-18 20:17:48

20

嘗試在設置元數據對象類型之前將輸入和輸出添加到會話中。

當您還沒有將相機連接到會話時,availableMetadataObjectTypes將爲空。

+2

這解決了它對我來說。 – lupz 2015-06-01 14:44:27

+1

這仍然是在iOS 11 w/Swift 4中的情況。首先啓動會話不是必需的。 – 2017-11-16 03:13:53