长按图片识别图中二维码:
1 // 长按图片识别二维码 2 3 UILongPressGestureRecognizer *QrCodeTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(QrCodeClick:)]; 4 5 [self.view addGestureRecognizer:QrCodeTap]; 6 7 8 9 - (void)QrCodeClick:(UILongPressGestureRecognizer *)pressSender { 10 11 12 13 if (pressSender.state != UIGestureRecognizerStateBegan) { 14 15 return;//长按手势只会响应一次 16 17 } 18 19 20 21 // MJPhoto *photo = _photos[_currentPhotoIndex]; 22 23 //截图 再读取 24 25 26 27 //第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,获取当前屏幕分辨率[UIScreen mainScreen].scale 28 29 UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 2.2); 30 31 32 33 CGContextRef context = UIGraphicsGetCurrentContext(); 34 35 36 37 [self.view.layer renderInContext:context]; 38 39 40 41 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 42 43 UIGraphicsEndImageContext(); 44 45 46 47 CIImage *ciImage = [[CIImage alloc] initWithCGImage:image.CGImage options:nil]; 48 49 CIContext *ciContext = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}]; // 软件渲染 50 51 CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:ciContext options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];// 二维码识别 52 53 54 55 NSArray *features = [detector featuresInImage:ciImage]; 56 57 58 59 if (features.count) { 60 61 62 63 for (CIQRCodeFeature *feature in features) { 64 65 NSLog(@"qrCodeUrl = %@",feature.messageString); // 打印二维码中的信息 66 67 qrCodeUrl = feature.messageString; 68 69 } 70 71 72 73 // 初始化弹框 第一个参数是设置距离底部的边距 74 75 alertview = [[RomAlertView alloc] initWithMainAlertViewBottomInset:0 Title:nil detailText:nil cancelTitle:nil otherTitles:[NSMutableArray arrayWithObjects:@"保存图片",@"识别图中二维码",nil]]; 76 77 alertview.tag = 10002; 78 79 // 设置弹框的样式 80 81 alertview.RomMode = RomAlertViewModeBottomTableView; 82 83 // 设置弹框从什么位置进入 当然也可以设置什么位置退出 84 85 [alertview setEnterMode:RomAlertEnterModeBottom]; 86 87 // 设置代理 88 89 [alertview setDelegate:self]; 90 91 // 显示 必须调用 和系统一样 92 93 [alertview show]; 94 95 } else { 96 97 NSLog(@"图片中没有二维码"); 98 99 } 100 101 102 103 } 104 105 106 107 #pragma mark -- RomAlertViewDelegate 弹框识别图中二维码 108 109 - (void)alertview:(RomAlertView *)alertview didSelectRowAtIndexPath:(NSIndexPath *)indexPath 110 111 { 112 113 if (alertview.tag == 10002) { 114 115 if ([alertview.otherTitles[indexPath.row] isEqualToString:@"保存图片"]) { 116 117 NSLog(@"保存图片"); 118 119 [self saveButtonPressed]; 120 121 }else if ([alertview.otherTitles[indexPath.row] isEqualToString:@"识别图中二维码"]){ 122 123 NSLog(@"识别图中二维码"); 124 125 126 127 // 隐藏 128 129 [alertview hide]; 130 131 [self leftBackButtonPressed]; 132 133 134 135 AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; 136 137 if([delegate.window.rootViewController isKindOfClass:[UITabBarController class]]){ 138 139 UITabBarController *tabBarController = (UITabBarController *)delegate.window.rootViewController; 140 141 UINavigationController *navigationController = [tabBarController selectedViewController]; 142 143 UIViewController *vc = navigationController.topViewController; 144 145 //对结果进行处理跳转网页 146 147 ADWebViewViewController *controller = [[ADWebViewViewController alloc] init]; 148 149 controller.m_url = qrCodeUrl; 150 151 controller.hidesBottomBarWhenPushed = YES; 152 153 [vc.navigationController pushViewController:controller animated:YES]; 154 155 } 156 157 } 158 159 } 160 161 }