iOS长按识别二维码
//
// FirstView.swift
// swift3.0
//
// Created by 韩海龙 on 16/12/1.
// Copyright © 2016年 hanhl. All rights reserved.
//
import UIKit
class FirstView: 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.png")
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.
}
}