Import UIKit
Class Firstviewcontroller: uiviewcontroller, Uiimagepickercontrollerdelegate, Uinavigationcontrollerdelegate {
var picker:uiimagepickercontroller!
@IBAction func button (_ Sender: UIButton) {
if uiimagepickercontroller. Issourcetypeavailable(. Photolibrary) {
Picker = Uiimagepickercontroller()
Picker. delegate = Self
Picker. sourcetype =. photolibrary
Self . present (picker, animated: True, completion: nil)
}
}
Func Imagepickercontroller (_ Picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [ String: any ]) {
Get the selected picture
let image = Info[uiimagepickercontrolleroriginalimage] as! UIImage
//Two-D code read
Dispatchqueue. Global (). Async {
Let imageData = uiimagepngrepresentation (image)
Let ciimage:ciimage=ciimage (data:imagedata!)!
let detector = cidetector(Oftype:cidetectortypeqrcode, Context:nil, options:[ Cidetectoraccuracy: Cidetectoraccuracyhigh])
Let feature = detector?. features (in:ciimage)
Let f = feature?. First as! ciqrcodefeature
print (f.messagestring?? "")
}
//Return
Picker. dismiss (animated: true, completion: {
(), Void in
})
}
}
The QR code of Swift cidetector identification album