Ios 将二维码加载到另一个视图控制器(Xcode)
我目前正在做一个测试应用程序,用户必须扫描一个二维码来做第一个问题,然后再扫描另一个二维码到第二个qn。现在,我的二维码扫描仪只能将二维码扫描到web url。我已经为我的问题做了布局和按钮,我只需要二维码链接到我的qn控制器。先谢谢你 这是我的QRScannerController.swiftIos 将二维码加载到另一个视图控制器(Xcode),ios,swift,xcode,qr-code,Ios,Swift,Xcode,Qr Code,我目前正在做一个测试应用程序,用户必须扫描一个二维码来做第一个问题,然后再扫描另一个二维码到第二个qn。现在,我的二维码扫描仪只能将二维码扫描到web url。我已经为我的问题做了布局和按钮,我只需要二维码链接到我的qn控制器。先谢谢你 这是我的QRScannerController.swift import UIKit import AVFoundation class QRScannerController: UIViewController, AVCaptureMetadataOutpu
import UIKit
import AVFoundation
class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
@IBOutlet var messageLabel:UILabel!
@IBOutlet var topbar: UIView!
var captureSession:AVCaptureSession?
var videoPreviewLayer:AVCaptureVideoPreviewLayer?
var qrCodeFrameView:UIView?
let supportedCodeTypes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeAztecCode,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode]
override func viewDidLoad() {
super.viewDidLoad()
// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice)
// Initialize the captureSession object.
captureSession = AVCaptureSession()
// Set the input device on the capture session.
captureSession?.addInput(input)
// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)
// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureMetadataOutput.metadataObjectTypes = supportedCodeTypes
// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)
// Start video capture.
captureSession?.startRunning()
// Move the message label and top bar to the front
view.bringSubview(toFront: messageLabel)
view.bringSubview(toFront: topbar)
// Initialize QR Code Frame to highlight the QR code
qrCodeFrameView = UIView()
if let qrCodeFrameView = qrCodeFrameView {
qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
qrCodeFrameView.layer.borderWidth = 2
view.addSubview(qrCodeFrameView)
view.bringSubview(toFront: qrCodeFrameView)
}
} catch {
// If any error occurs, simply print it out and don't continue any more.
print(error)
return
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - AVCaptureMetadataOutputObjectsDelegate Methods
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
messageLabel.text = "No QR/barcode is detected"
return
}
// Get the metadata object.
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if supportedCodeTypes.contains(metadataObj.type) {
// If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds
if metadataObj.stringValue != nil {
let url = URL(string: metadataObj.stringValue)!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
}
}
这是我的问题1控制器
import UIKit
class Quiz1Controller: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我建议您创建一个帮助文件,将QRCode扫描与UIViewController分开。这将清理代码并实现可重用性 回答你的问题。您可以更改方法
func captureOutput(\ucaptureoutput:AVCaptureOutput!、didOutputMetadataObjects:[Any]!、from connection:AVCaptureConnection!){
以处理扫描QRCode的输出
实际上,我实现了一个委托来从QRCodeHelper返回值,但是您也可以在不使用文件的情况下调用函数
为此,请创建一个新函数func qrScanDidScanCode(code:String)
,并在captureOutput
中调用它,如下所示
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
var QRCode: String?
for metadata in metadataObjects as! [AVMetadataObject] {
if metadata.type == AVMetadataObjectTypeQRCode {
QRCode = (metadata as! AVMetadataMachineReadableCodeObject).stringValue
}
}
if let qr = QRCode {
qrScanDidScanCode(qr)
}
}
qr变量将QRCodes值作为字符串保存,现在您可以在任何地方使用它
如果要在下一个viewController中使用QRCodes的字符串值,请使用segues方法执行和准备
在当前UIViewController中创建一个新属性:var selectedQRCode:String?
在设置selectedQRCode后,在qrscancode()
中设置此属性,并调用performsgue
在准备格中,执行以下操作:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? SecondViewController {
destination.qrCode = selectedQRCode
}
}
我建议您创建一个帮助文件,将QRCode扫描与UIViewController分离。这将清理您的代码并实现可重用性 回答您的问题。您可以更改方法
func captureOutput(\uCaptureOutput:AVCaptureOutput!、didOutputMetadataObjects metadataObjects:[Any]!、from connection:AVCaptureConnection!){
以处理扫描QRCode的输出
实际上,我实现了一个委托来从QRCodeHelper返回值,但是您也可以在不使用文件的情况下调用函数
为此,请创建一个新函数func qrScanDidScanCode(code:String)
,并在captureOutput
中调用它,如下所示
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
var QRCode: String?
for metadata in metadataObjects as! [AVMetadataObject] {
if metadata.type == AVMetadataObjectTypeQRCode {
QRCode = (metadata as! AVMetadataMachineReadableCodeObject).stringValue
}
}
if let qr = QRCode {
qrScanDidScanCode(qr)
}
}
qr变量将QRCodes值作为字符串保存,现在您可以在任何地方使用它
如果要在下一个viewController中使用QRCodes的字符串值,请使用segues方法执行和准备
在当前UIViewController中创建一个新属性:var selectedQRCode:String?
在设置selectedQRCode后,在qrscancode()
中设置此属性,并调用performsgue
在准备格中,执行以下操作:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? SecondViewController {
destination.qrCode = selectedQRCode
}
}
一旦你有了数据,不要打开URL;执行到下一个视图控制器的顺序RMM你如何为scannerccontroller做这件事?是通过二维码文本吗?一旦你有了数据,不要打开URL;执行到下一个视图控制器的顺序RMM你如何为scannerccontroller做这件事?是通过二维码文本吗?对不起,tom,我对Xcode和我都很陌生有10个UIVIEW控制器需要QRSCNANNER控制器来链接。我如何一步一步地做这件事?(如果它对于每个UIVIEW控制器都有相同的QR代码值,你可以考虑在UsRead Debug中保存它。标准。SET(QRcode,FrKEY:“QRCODEVALUE”)对不起,汤姆,我对Xcode很陌生,我有10个UIVIEW控制器,需要QRSCNANNER控制器来链接。我如何一步一步地做这件事?(如果它对于每个UIVIEW控制器都有相同的QR代码值,你可以考虑在USER Debug中保存它。标准。SET(QRcode,FROKE:“QRCODEVALUE”)。