Swfit3.0 实现扫描二维码 , 不走代理方法或者是没有扫描到数据??
//输入设备 fileprivate lazy var input: AVCaptureDeviceInput? = { let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) return try? AVCaptureDeviceInput(device: device) }() // 输出对象 fileprivate lazy var output : AVCaptureMetadataOutput = { let output = AVCaptureMetadataOutput() output.metadataObjectTypes = output.availableMetadataObjectTypes output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) let centerViewFrame = self.centerView.frame let screenFrame = UIScreen.main.bounds // 测试发现系统是按照右上角作为参照, 并不是以左上角 let x = centerViewFrame.origin.y / screenFrame.size.height let y = centerViewFrame.origin.x / screenFrame.size.width let width = centerViewFrame.size.height / screenFrame.size.height let height = centerViewFrame.size.width / screenFrame.size.width output.rectOfInterest = CGRect(x: x, y: y, width: width, height: height) return output }() // 会话 fileprivate lazy var session = AVCaptureSession() // 预览图层 fileprivate lazy var previewlayer: AVCaptureVideoPreviewLayer = { let layer = AVCaptureVideoPreviewLayer(session: self.session) layer?.frame = self.view.bounds return layer! }()
// MARK: - 二维码扫描 extension QRCodeViewController: AVCaptureMetadataOutputObjectsDelegate { fileprivate func setupScanQRCode() { if !session.canAddInput(input) || !session.canAddOutput(output) { return } session.addInput(input) session.addOutput(output) view.layer.insertSublayer(previewlayer, at: 0) session.startRunning() } func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { for objc in metadataObjects { let tempObjc = objc as! AVMetadataMachineReadableCodeObject C优艾设计网_PS交流CLog(tempObjc.stringValue) } } }
不走后面的代理方法, 如果是因为没有扫描到数据而没有走代理方法, 那这里写的有什么问题吗? 望指出
精彩评论