下記ページを参考に2次元バーコード読み取りをswift2で書いたのですが、実機デバック時にコンソール画面の「fatal error: unexpectedly found nil while unwrapping an Optional value」と言うエラーが出てきます。どこをoptional型にすればいいのでしょうか?xcode7beta5を使用しています。
http://swift-salaryman.com/avmetadataobject.php
swift
1import UIKit 2import AVFoundation 3 4 5class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { 6 7 let session : AVCaptureSession = AVCaptureSession() 8 var previewLayer : AVCaptureVideoPreviewLayer! 9 var highlightView : UIView = UIView() 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 // Allow the view to resize freely 15 self.highlightView.autoresizingMask = [UIViewAutoresizing.FlexibleTopMargin, UIViewAutoresizing.FlexibleBottomMargin, UIViewAutoresizing.FlexibleLeftMargin, UIViewAutoresizing.FlexibleRightMargin] 16 17 // Select the color you want for the completed scan reticle 18 self.highlightView.layer.borderColor = UIColor.greenColor().CGColor 19 self.highlightView.layer.borderWidth = 3 20 21 // Add it to our controller's view as a subview. 22 self.view.addSubview(self.highlightView) 23 24 do { 25 let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 26 _ = try AVCaptureDeviceInput(device: captureDevice) 27 // Do the rest of your work... 28 } catch let error as NSError { 29 // Handle any errors 30 print(error) 31 } 32 // If our input is not nil then add it to the session, otherwise we're kind of done! 33 34 35 let output = AVCaptureMetadataOutput() 36 output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) 37 session.addOutput(output) 38 output.metadataObjectTypes = output.availableMetadataObjectTypes 39 40 41 previewLayer = AVCaptureVideoPreviewLayer(layer: session) as AVCaptureVideoPreviewLayer 42 previewLayer.frame = self.view.bounds 43 previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 44 self.view.layer.addSublayer(previewLayer) 45 46 // Start the scanner. You'll have to end it yourself later. 47 session.startRunning() 48 49 } 50 51 // This is called when we find a known barcode type with the camera. 52 func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { 53 54 var highlightViewRect = CGRectZero 55 56 var barCodeObject : AVMetadataObject! 57 58 var detectionString : String! 59 60 let barCodeTypes = [AVMetadataObjectTypeUPCECode, 61 AVMetadataObjectTypeCode39Code, 62 AVMetadataObjectTypeCode39Mod43Code, 63 AVMetadataObjectTypeEAN13Code, 64 AVMetadataObjectTypeEAN8Code, 65 AVMetadataObjectTypeCode93Code, 66 AVMetadataObjectTypeCode128Code, 67 AVMetadataObjectTypePDF417Code, 68 AVMetadataObjectTypeQRCode, 69 AVMetadataObjectTypeAztecCode 70 ] 71 72 73 // The scanner is capable of capturing multiple 2-dimensional barcodes in one scan. 74 for metadata in metadataObjects { 75 76 for barcodeType in barCodeTypes { 77 78 if metadata.type == barcodeType { 79 barCodeObject = self.previewLayer.transformedMetadataObjectForMetadataObject(metadata as! AVMetadataMachineReadableCodeObject) 80 81 highlightViewRect = barCodeObject.bounds 82 83 detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue 84 85 self.session.stopRunning() 86 break 87 } 88 89 } 90 } 91 92 print(detectionString) 93 self.highlightView.frame = highlightViewRect 94 self.view.bringSubviewToFront(self.highlightView) 95 96 } 97} 98
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。