IOS-长按图片识别二维码

2020-12-01  本文已影响0人  你好哈喽哈喽

前言

项目中用到长按图片识别二维码,并跳转,看了一些网上的,然后自己把需要的拿出来分享一下

下面是代码

[_longPressGestureEWM addTarget:self action:@selector(dealLongPress:)];          [self getInfoRequest];

pragma mark->长按识别二维码

-(void)dealLongPress:(UIGestureRecognizer*)gesture{

// 0.创建上下文

CIContext *context = [[CIContext alloc] init];

// 1.创建一个探测器

CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];

// 2.直接开始识别图片,获取图片特征

CIImage *imageCI = [[CIImage alloc] initWithImage:self.erweimaImageView.image]; NSArray *features = [detector featuresInImage:imageCI];

CIQRCodeFeature *codef = (CIQRCodeFeature *)features.firstObject;

// 弹框

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"识别图中二维码" message:nil preferredStyle:UIAlertControllerStyleAlert];

[alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消"); }]];

__weak typeof (self)weakSelf = self;

[alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:codef.messageString]];        

[weakSelf presentViewController:safariVC animated:YES completion:nil]; }]];

[self presentViewController:alertC animated:YES completion:nil]; }

上一篇 下一篇

猜你喜欢

热点阅读