swift實(shí)現(xiàn)二維碼掃描

備注:使用第三方庫[swiftScan]
(https://github.com/MxABC/swiftScan/blob/master/README-Chinese.md)

1. 步驟

(1)使用cocoapods安裝swiftScan第三方庫

pod 'swiftScan'    #二維碼掃描

說明:使用cocoapods安裝第三方庫請(qǐng)參考Cocoapods管理IOS依賴庫

(2)編譯后引入swiftScan

import swiftScan

(3)遵從LBXScanViewController協(xié)議

遵從LBXScanViewController協(xié)議.png

運(yùn)行報(bào)錯(cuò):Cannot Record。
分析:在模擬器中沒有攝像頭不能錄制。
(4)重寫handleCodeResult方法

override func handleCodeResult(arrayResult: [LBXScanResult]) {
        if let result = arrayResult.first {
            let msg = result.strScanned
            print("掃描結(jié)果:" + msg!)

        }
    }

(5)自定義掃描動(dòng)畫

//掃描動(dòng)畫:在github下載項(xiàng)目,復(fù)制CodeScan.bundle獲取圖片
var style = LBXScanViewStyle()
style.anmiationStyle = .NetGrid
style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_part_net")
  scanStyle = style

2. 具體實(shí)現(xiàn)

(1)界面設(shè)計(jì)


界面設(shè)計(jì).png

(2)掃一掃對(duì)應(yīng)的視圖控制器ScanViewController.swift

//
//  ScanViewController.swift
//  JackUChat
//
//  Created by 徐云 on 2019/2/25.
//  Copyright ? 2019 Liy. All rights reserved.
//

import UIKit
import swiftScan

class ScanViewController: LBXScanViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
       //自定義視圖導(dǎo)航標(biāo)題
        self.title = "二維碼/條碼"
        navigationController?.navigationBar.barStyle = .blackTranslucent
        navigationController?.navigationBar.tintColor = UIColor.white
        
        //掃描動(dòng)畫:在github下載項(xiàng)目,復(fù)制CodeScan.bundle獲取圖片
        var style = LBXScanViewStyle()
        style.anmiationStyle = .NetGrid
        style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_part_net")//引用bundle中的圖片
        scanStyle = style
        
    }
    
    override func handleCodeResult(arrayResult: [LBXScanResult]) {
        if let result = arrayResult.first {
            let msg = result.strScanned
            print("掃描結(jié)果:" + msg!)

        }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

3. 效果展示

二維碼掃描界面.png

4. 返回上級(jí)界面時(shí)速度慢

(1)分析:組件作者可能是按O-C的思路來寫的swiftScan庫的,swift時(shí)代不需要銷毀(反初始化)的處理。


控制臺(tái)信息.png

(2)解決方案:修改LBXScanViewController源碼,注釋viewWillDisappear()函數(shù)中的代碼


解決方案.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容