iOS长按图片识别二维码

前言

在iOS的APP中有时须要识别图片中的二维码来获取信息,相似微信,支付宝都有这个功能。
其实苹果自己有识别的功能,因此只须要几句代码便可实现。

**我简单说一下个人步骤**
复制代码

预览图

预览图

代码以下

//建立上下文context
    CIContext *context = [[CIContext alloc] init];
    //建立探测器
    CIDetector *QRCodeDetector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];
    //识别图片,获取特征
    CIImage *imgCI = [[CIImage alloc] initWithImage:imgV.image];
    NSArray *features = [QRCodeDetector featuresInImage:imgCI];
    
    //判断是否识别到二维码
    if (features.count > 0) {
     
        //有二维码
        CIQRCodeFeature *qrcodeFeature = (CIQRCodeFeature *)features[0];
        
        __weak typeof(self) weakSelf = self;
        UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"" message:@"识别内容" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *dicern = [UIAlertAction actionWithTitle:@"识别图中二维码" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            ResultViewController *resultVC = [[ResultViewController alloc] init];
            resultVC.content = qrcodeFeature.messageString;
            [weakSelf.navigationController pushViewController:resultVC animated:YES];
            
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [sheet addAction:dicern];
        [sheet addAction:cancel];
        [self presentViewController:sheet animated:YES completion:nil];
        
    }else{
     
        //没有二维码,这只是一张普通的图片
        NSLog(@"|没有二维码,这只是一张普通的图片|");
    }
复制代码
好了,就这么简单!识别断定有得时候才弹窗(我不会告诉大家我是为了有个等级标志的)
第一次写文章,格式都不会,渣渣的我只能发点简单的,给有须要的人复制代码
相关文章
相关标签/搜索