实现长按识别webview中的二维码

@property (nonatomic,copy) NSString *imgURL;

   UIWebView *_web;
   NSInteger _gesState;

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [MBProgressHUD hideHUD];
    //实现图片长按识别功能
    static NSString* const kTouchJavaScriptString=
    @"document.ontouchstart=function(event){\
    x=event.targetTouches[0].clientX;\
    y=event.targetTouches[0].clientY;\
    document.location=\"myweb:touch:start:\"+x+\":\"+y;};\
    document.ontouchmove=function(event){\
    x=event.targetTouches[0].clientX;\
    y=event.targetTouches[0].clientY;\
    document.location=\"myweb:touch:move:\"+x+\":\"+y;};\
    document.ontouchcancel=function(event){\
    document.location=\"myweb:touch:cancel\";};\
    document.ontouchend=function(event){\
    document.location=\"myweb:touch:end\";};";
    [webView stringByEvaluatingJavaScriptFromString:kTouchJavaScriptString];//注入js方法
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // add by zcj 实现图片长按识别功能
    NSString *requestString =  request.URL.absoluteString;

    NSArray *components = [requestString componentsSeparatedByString:@":"];
    if ([components count] > 1 && [(NSString *)[components objectAtIndex:0]
                                   isEqualToString:@"myweb"]) {
        if([(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])
        {
            //NSLog(@"you are touching!");
            if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"start"])
            {
                _gesState = UIGestureRecognizerStateBegan;

                float ptX  =[[components objectAtIndex:3]floatValue];
                float ptY =[[components objectAtIndex:4]floatValue];

                NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", ptX, ptY];
                NSString * tagName = [_web stringByEvaluatingJavaScriptFromString:js];

                if ([tagName isEqualToString:@"IMG"]) {
                    self.imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", ptX, ptY];
                }
                if (self.imgURL) {
                    [self performSelector:@selector(handleLongTouch) withObject:nil afterDelay:1.0];
                }
            }
            else if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"move"])
            {

                _gesState = UIGestureRecognizerStateChanged;

            }
        }
        else if ([(NSString*)[components objectAtIndex:2]isEqualToString:@"end"]) {

            _gesState = UIGestureRecognizerStateEnded;

        }
    }// ]end

    return YES;
}
- (void)handleLongTouch {

    if (self.imgURL && _gesState == UIGestureRecognizerStateBegan) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

        NSString *urlToSave = [_web stringByEvaluatingJavaScriptFromString:self.imgURL];

        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlToSave]];
        UIImage* image = [UIImage imageWithData:data];

        NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                                 @"CIDetectorAccuracy", @"CIDetectorAccuracyHigh",nil];
        CIDetector *detector = nil;
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
            detector = [CIDetector detectorOfType:CIDetectorTypeQRCode
                                          context:nil
                                          options:options];
        NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]] ;
        // 识别图中二维码
        UIAlertAction *judgeCode = [UIAlertAction actionWithTitle:@"识别图中二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            CIQRCodeFeature *feature = [features objectAtIndex:0];
            NSString *scannedResult = feature.messageString;

            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scannedResult]]){
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:scannedResult]];
            }else{
                [MBProgressHUD showError:@"无法识别的网址"];
            }
        }];
        // 保存图片到手机
        UIAlertAction *saveImage = [UIAlertAction actionWithTitle:@"保存到手机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }];
        // 取消
        UIAlertAction *cancell = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];
        if (features.count >= 1) {
            [alertController addAction:judgeCode];
        }
        [alertController addAction:saveImage];
        [alertController addAction:cancell];
        [self presentViewController:alertController animated:YES completion:nil];

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值