Swift3.0仿支付寶二維碼掃描效果
更新時間:2017年02月20日 16:30:00 作者:Stevin三天三夜的專欄
這篇文章主要為大家詳細(xì)介紹了Swift3.0仿支付寶二維碼掃描效果的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Swift3.0二維碼掃描的具體代碼,供大家參考,具體內(nèi)容如下
關(guān)鍵代碼
import AVFoundation //獲取攝像設(shè)備 let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) do { //創(chuàng)建輸入,輸出流 let input = try AVCaptureDeviceInput.init(device: device) let output = AVCaptureMetadataOutput() output.rectOfInterest = CGRect(x: 0.1, y: 0, width: 0.9, height: 1) //設(shè)置代理,并且在主線程刷新 output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) //初始化鏈接對象 / 高質(zhì)量采集率 session.canSetSessionPreset(AVCaptureSessionPresetHigh) session.addInput(input) session.addOutput(output) //先添加輸入輸出流,后設(shè)置支持的掃碼所支持的格式,不然報錯如下:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] Unsupported type found - use -availableMetadataObjectTypes' //http://stackoverflow.com/questions/31063846/avcapturemetadataoutput-setmetadataobjecttypes-unsupported-type-found //設(shè)置掃碼支持的編碼格式 output.metadataObjectTypes = [AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code] let layer = AVCaptureVideoPreviewLayer(session: session) layer?.videoGravity = AVLayerVideoGravityResizeAspectFill layer?.frame = view.layer.bounds view.layer.insertSublayer(layer!, at: 0) //開始捕捉 session.startRunning() } catch let error as NSError { print("errorInfo\(error.domain)") } /// 實現(xiàn)代理方法 extension ScanCodeController:AVCaptureMetadataOutputObjectsDelegate { func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { if metadataObjects.count > 0 { session.stopRunning() let object = metadataObjects[0] let string: String = (object as AnyObject).stringValue if let url = URL(string: string) { if UIApplication.shared.canOpenURL(url) { //去打開地址鏈接 _ = self.navigationController?.popViewController(animated: true) UIApplication.shared.open(url) } else { //獲取非鏈接結(jié)果 let alertViewController = UIAlertController(title: "掃描結(jié)果", message: (object as AnyObject).stringValue, preferredStyle: .alert) let actionCancel = UIAlertAction(title: "退出", style: .cancel, handler: { (action) in _ = self.navigationController?.popViewController(animated: true) }) let actinSure = UIAlertAction(title: "再次掃描", style: .default, handler: { (action) in self.session.startRunning() }) alertViewController.addAction(actionCancel) alertViewController.addAction(actinSure) self.present(alertViewController, animated: true, completion: nil) } } } } }
DEMO效果:
下載地址:DEMO
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
用Swift構(gòu)建一個簡單的iOS郵件應(yīng)用的方法
這篇文章主要介紹了用Swift構(gòu)建一個簡單的iOS郵件應(yīng)用的方法,包括查看和標(biāo)記已讀等基本的郵件應(yīng)用功能,需要的朋友可以參考下2015-07-07Swift學(xué)習(xí)教程之SQLite的基礎(chǔ)使用
這篇文章主要給大家介紹了關(guān)于Swift學(xué)習(xí)教程之SQLite的基礎(chǔ)使用,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Swift SQLite具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Spring中BeanFactory與FactoryBean的區(qū)別解讀
這篇文章主要介紹了Spring中BeanFactory與FactoryBean的區(qū)別解讀,Java的BeanFactory是Spring框架中的一個接口,它是用來管理和創(chuàng)建對象的工廠接口,在Spring中,我們可以定義多個BeanFactory來管理不同的組件,需要的朋友可以參考下2023-12-12Swift中的可選項Optional解包方式實現(xiàn)原理
這篇文章主要為大家介紹了Swift中的可選項Optional解包方式實現(xiàn)原理示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03