IOS-长按图片识别二维码

前言

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

下面是代码

    [_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]; }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容