iOS - 长按图片识别图中二维码
Posted 公羽寒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS - 长按图片识别图中二维码相关的知识,希望对你有一定的参考价值。
// 长按图片识别二维码
UILongPressGestureRecognizer *QrCodeTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(QrCodeClick:)];
[self.view addGestureRecognizer:QrCodeTap];
- (void)QrCodeClick:(UILongPressGestureRecognizer *)pressSender {
if (pressSender.state != UIGestureRecognizerStateBegan) {
return;//长按手势只会响应一次
}
// MJPhoto *photo = _photos[_currentPhotoIndex];
//截图 再读取
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CIImage *ciImage = [[CIImage alloc] initWithCGImage:image.CGImage options:nil];
CIContext *ciContext = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}]; // 软件渲染
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:ciContext options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];// 二维码识别
NSArray *features = [detector featuresInImage:ciImage];
if (features.count) {
for (CIQRCodeFeature *feature in features) {
NSLog(@"qrCodeUrl = %@",feature.messageString); // 打印二维码中的信息
qrCodeUrl = feature.messageString;
}
// 初始化弹框 第一个参数是设置距离底部的边距
alertview = [[RomAlertView alloc] initWithMainAlertViewBottomInset:0 Title:nil detailText:nil cancelTitle:nil otherTitles:[NSMutableArray arrayWithObjects:@"保存图片",@"识别图中二维码",nil]];
alertview.tag = 10002;
// 设置弹框的样式
alertview.RomMode = RomAlertViewModeBottomTableView;
// 设置弹框从什么位置进入 当然也可以设置什么位置退出
[alertview setEnterMode:RomAlertEnterModeBottom];
// 设置代理
[alertview setDelegate:self];
// 显示 必须调用 和系统一样
[alertview show];
} else {
NSLog(@"图片中没有二维码");
}
}
#pragma mark -- RomAlertViewDelegate 弹框识别图中二维码
- (void)alertview:(RomAlertView *)alertview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (alertview.tag == 10002) {
if ([alertview.otherTitles[indexPath.row] isEqualToString:@"保存图片"]) {
NSLog(@"保存图片");
[self saveButtonPressed];
}else if ([alertview.otherTitles[indexPath.row] isEqualToString:@"识别图中二维码"]){
NSLog(@"识别图中二维码");
// 隐藏
[alertview hide];
[self leftBackButtonPressed];
AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
if([delegate.window.rootViewController isKindOfClass:[UITabBarController class]]){
UITabBarController *tabBarController = (UITabBarController *)delegate.window.rootViewController;
UINavigationController *navigationController = [tabBarController selectedViewController];
UIViewController *vc = navigationController.topViewController;
//对结果进行处理跳转网页
ADWebViewViewController *controller = [[ADWebViewViewController alloc] init];
controller.m_url = qrCodeUrl;
controller.hidesBottomBarWhenPushed = YES;
[vc.navigationController pushViewController:controller animated:YES];
}
}
}
}
以上是关于iOS - 长按图片识别图中二维码的主要内容,如果未能解决你的问题,请参考以下文章