gpt4 book ai didi

ios - 适用于 IOS 的 QRCode 扫描器应用程序出现 fatal error

转载 作者:行者123 更新时间:2023-11-30 13:10:01 24 4
gpt4 key购买 nike

我正在开发一个适用于 IOS 的 QRCode 扫描仪应用程序,具有两个 View 。当我在 iPad 2 上编译并启动该应用程序并单击工具栏上的按钮时,我在两个类上都收到一个带有子代码 0xe77ffdefe 的 EXC_BREAKPOINT。

class ViewController: UIViewController {

@IBAction func showQRView(sender: AnyObject){

let secondView:QRCodeViewController = QRCodeViewController()
self.presentViewController(secondView, animated: true, completion: nil)

}//end of showQRView



override func viewDidLoad() {
super.viewDidLoad()





}



override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()



}}

二等

class QRCodeViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate {
@IBAction func closeView(sender:AnyObject)
self.dismissViewControllerAnimated(true, completion: nil)
}

@IBOutlet weak var messageLabel:UILabel!

var captureSession:AVCaptureSession?
var videoPreviewLayer:AVCaptureVideoPreviewLayer?
var qrCodeFrameView:UIView?a

let supportedBarCodes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeUPCECode, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeAztecCode]





override func viewDidLoad() {
super.viewDidLoad()

messageLabel.frame = CGRectMake(0, 0, 768, 71)
messageLabel.backgroundColor = UIColor.blackColor()
messageLabel.textColor = UIColor(colorLiteralRed: 0.0, green: 255.0, blue: 0.0, alpha: 1.0)
messageLabel.textAlignment = NSTextAlignment.Center

let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

do {
let input = try AVCaptureDeviceInput(device: captureDevice)

captureSession = AVCaptureSession()
captureSession?.addInput(input)

let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)

captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())

captureMetadataOutput.metadataObjectTypes = supportedBarCodes

videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)

captureSession?.startRunning()

view.bringSubviewToFront(messageLabel)

qrCodeFrameView = UIView()

if let qrCodeFrameView = qrCodeFrameView {
qrCodeFrameView.layer.borderColor = UIColor.greenColor().CGColor
qrCodeFrameView.layer.borderWidth = 2
view.addSubview(qrCodeFrameView)
view.bringSubviewToFront(qrCodeFrameView)
}

} catch {
print(error)
return
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRectZero
messageLabel.text = "No QR code is detected"
return
}


let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject


if supportedBarCodes.contains(metadataObj.type) {

let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds

if metadataObj.stringValue != nil {
messageLabel.text = metadataObj.stringValue
}
}
}

}

最佳答案

问题是,调用 QRCodeViewController() 会创建一个 QRCodeViewController 实例,该实例对您在界面生成器中设计的 View 一无所知。相反,您需要告诉 Storyboard为您实例化一个实例。

打开 Storyboard并选择您的 QRCodeViewController 场景。打开右侧 Pane (实用程序),然后单击顶部的身份检查器图标(看起来像一张小身份证)。在“ Storyboard ID”框中填写唯一的字符串。

现在,回到代码中,将 @IBAction func showQRView(sender: AnyObject) 函数的内容替换为以下内容

let storyboard = UIStoryboard(name: "STORY_BOARD_NAME_HERE", bundle: nil)
let secondView = storyboard.instantiateViewControllerWithIdentifier("ID_YOU_SET_EARLIER_HERE") as! QRCodeViewController
self.presentViewController(secondView, animated: true, completion: nil)

关于ios - 适用于 IOS 的 QRCode 扫描器应用程序出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38843687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com