欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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 4.2使用self做為變量名淺析

    Swift 4.2使用self做為變量名淺析

    這篇文章主要給大家介紹了關(guān)于Swift 4.2使用self做為變量名的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Swift泛型Generics淺析講解

    Swift泛型Generics淺析講解

    泛型代碼讓你能根據(jù)你所定義的要求,寫出可以用于任何類型的靈活的、可復(fù)用的函數(shù)。泛型是 Swift 最強大的特性之一,很多 Swift 標(biāo)準(zhǔn)庫是基于泛型代碼構(gòu)建的
    2022-08-08
  • 用Swift構(gòu)建一個簡單的iOS郵件應(yīng)用的方法

    用Swift構(gòu)建一個簡單的iOS郵件應(yīng)用的方法

    這篇文章主要介紹了用Swift構(gòu)建一個簡單的iOS郵件應(yīng)用的方法,包括查看和標(biāo)記已讀等基本的郵件應(yīng)用功能,需要的朋友可以參考下
    2015-07-07
  • 詳解Swift編程中的常量和變量

    詳解Swift編程中的常量和變量

    這篇文章主要介紹了Swift編程中的常量和變量,是Swift入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11
  • 超全面的Swift編碼規(guī)范(推薦)

    超全面的Swift編碼規(guī)范(推薦)

    這篇文章主要給大家介紹了關(guān)于Swift編碼規(guī)范的相關(guān)資料,文中介紹的非常詳細(xì),對大家開發(fā)swift具有一定的參考價值,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。
    2017-03-03
  • Swift學(xué)習(xí)教程之SQLite的基礎(chǔ)使用

    Swift學(xué)習(xí)教程之SQLite的基礎(chǔ)使用

    這篇文章主要給大家介紹了關(guān)于Swift學(xué)習(xí)教程之SQLite的基礎(chǔ)使用,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Swift SQLite具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 詳解Swift編程中的for循環(huán)的編寫方法

    詳解Swift編程中的for循環(huán)的編寫方法

    這篇文章主要介紹了Swift編程中的for循環(huán)的編寫方法,包括相關(guān)的for...in循環(huán),需要的朋友可以參考下
    2015-11-11
  • Swift中的訪問控制和protected

    Swift中的訪問控制和protected

    這篇文章主要介紹了Swift中的訪問控制和protected,本文主要講解為什么Swift沒有類似protected的選項,需要的朋友可以參考下
    2015-05-05
  • Spring中BeanFactory與FactoryBean的區(qū)別解讀

    Spring中BeanFactory與FactoryBean的區(qū)別解讀

    這篇文章主要介紹了Spring中BeanFactory與FactoryBean的區(qū)別解讀,Java的BeanFactory是Spring框架中的一個接口,它是用來管理和創(chuàng)建對象的工廠接口,在Spring中,我們可以定義多個BeanFactory來管理不同的組件,需要的朋友可以參考下
    2023-12-12
  • Swift中的可選項Optional解包方式實現(xiàn)原理

    Swift中的可選項Optional解包方式實現(xiàn)原理

    這篇文章主要為大家介紹了Swift中的可選項Optional解包方式實現(xiàn)原理示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評論