二维码---长按识别 swift

前端之家收集整理的这篇文章主要介绍了二维码---长按识别 swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

swift 3.0 Xcode 8.1

import UIKit

class ViewController: UIViewController {
    
    var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        imageView = UIImageView.init(frame: CGRect.init(x: 100,y: 100,width: 200,height: 200))
        imageView.image = UIImage.init(named: "123.jpeg")
        imageView.isUserInteractionEnabled = true
        self.view.addSubview(imageView)
        
        //长按识别二维码
        let longPress = UILongPressGestureRecognizer.init(target: self,action: #selector(QRLongPress(gesture:)))
        longPress.minimumPressDuration = 1
        imageView.addGestureRecognizer(longPress)
        
    }

    //MARK: - 长按二维码识别
    func QRLongPress(gesture: UILongPressGestureRecognizer) {
        
        if (gesture.state == UIGestureRecognizerState.began) {
            
            //1.初始化扫描仪,设置设别类型和识别质量
            let options = ["IDetectorAccuracy" : CIDetectorAccuracyHigh]
            let detector: CIDetector = CIDetector.init(ofType: "CIDetectorTypeQRCode",context: nil,options: options)!
            //2.扫描获取的特征组
            let features = detector.features(in: CIImage.init(cgImage: (self.imageView.image?.cgImage)!))
            //3.获取扫描结果
            let feature = features[0] as! CIQRCodeFeature
            let scannedResult = feature.messageString
            //4.获取之后的操作
            print(scannedResult!)
            
        } else if (gesture.state == UIGestureRecognizerState.ended) {
            
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
原文链接:https://www.f2er.com/swift/322586.html

猜你在找的Swift相关文章