下記ページを参考に2次元バーコード読み取りをswift2で書いたのですが、実機デバック時にコンソール画面の「fatal error: unexpectedly found nil while unwrapping an Optional value」と言うエラーが出てきます。どこをoptional型にすればいいのでしょうか?xcode7beta5を使用しています。
追記
他のサイトなどを参考にコードを変更してみました。そしたら実機で起動するが真っ白な画面となりカメラが起動しません。コードの修正をお願いします。
[http://swift-salaryman.com/avmetadataobject.php ][1]
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
let session : AVCaptureSession! = AVCaptureSession()
var previewLayer : AVCaptureVideoPreviewLayer!
var highlightView : UIView = UIView()
// デバイス.
var captureDevice : AVCaptureDevice!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
session.sessionPreset = AVCaptureSessionPresetLow
// Allow the view to resize freely
self.highlightView.autoresizingMask = [UIViewAutoresizing.FlexibleTopMargin, UIViewAutoresizing.FlexibleBottomMargin, UIViewAutoresizing.FlexibleLeftMargin, UIViewAutoresizing.FlexibleRightMargin]
// Select the color you want for the completed scan reticle
self.highlightView.layer.borderColor = UIColor.greenColor().CGColor
self.highlightView.layer.borderWidth = 3
// Add it to our controller's view as a subview.
self.view.addSubview(self.highlightView)
// デバイス一覧の取得.
let devices = AVCaptureDevice.devices()
// バックカメラをcaptureDeviceに格納.
for device in devices{
if(device.position == AVCaptureDevicePosition.Back){
captureDevice = device as! AVCaptureDevice
if captureDevice != nil {
print("Capture device found")
}
}
}
do {
captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
let input = try AVCaptureDeviceInput(device: captureDevice)
// Do the rest of your work...
} catch let error as NSError {
// Handle any errors
print(error)
}
// If our input is not nil then add it to the session, otherwise we're kind of done!
let output = AVCaptureMetadataOutput()
output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
session.addOutput(output)
output.metadataObjectTypes = output.availableMetadataObjectTypes
previewLayer = AVCaptureVideoPreviewLayer(session: session)
self.view.layer.addSublayer(previewLayer)
previewLayer?.frame = self.view.layer.frame
// Start the scanner. You'll have to end it yourself later.
session.startRunning()
}
// This is called when we find a known barcode type with the camera.
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
var highlightViewRect = CGRectZero
var barCodeObject : AVMetadataObject!
var detectionString : String!
let barCodeTypes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode
]
// The scanner is capable of capturing multiple 2-dimensional barcodes in one scan.
for metadata in metadataObjects {
for barcodeType in barCodeTypes {
if metadata.type == barcodeType {
barCodeObject = self.previewLayer.transformedMetadataObjectForMetadataObject(metadata as! AVMetadataMachineReadableCodeObject)
highlightViewRect = barCodeObject.bounds
detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue
self.session.stopRunning()
break
}
}
}
print(detectionString)
self.highlightView.frame = highlightViewRect
self.view.bringSubviewToFront(self.highlightView)
}
}
let deviceInput = try AVCaptureDeviceInput(device: captureDevice)
で取得した入力を、session.addInput(deviceInput)
に渡すだけです。AVFoundation
の基礎が理解できていないためだと思いますので、まずはAVFoundationプログラミングガイドなどのリファレンスを読まれると良いと思います。「コードの修正をお願いします」といったような質問は好まれないと思います。