在做点击相册识别二维码识别不了,折腾了大半天,终于从坑里爬出来 ,下面分享一下填坑过程
在创建相册和获取图片的时候添加上面的代码。然后用取出来的图片作为参数进行二维码识别,下面封装了一个类方法:
public func recognizeQRCode() -> String?
{
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy : CIDetectorAccuracyHigh])
guard let imageData = UIImageJPEGRepresentation(self, 1) else {
return nil
}
guard let features = detector?.features(in: CoreImage.CIImage.init(data: imageData)!) else {
return nil
}
guard features.count > 0 else { return nil }
let feature = features.first as? CIQRCodeFeature
return feature?.messageString
}
具体调用如下所示:
let image = info[UIImagePickerControllerEditedImage] as! UIImage
guard let recognizeResult = image.recognizeQRCode() else {
ToastUtil.showHud("无法识别")
return
}
if recognizeResult.count > 0 {
print("识别结果--(recognizeResult)")
} else {
ToastUtil.showHud("无法识别")
}