ios APPweb网页长按识别二维码和保存图片

知识点:oc和js的交互 、系统原生类二维码识别

核心思想:通过oc向web网页注入js代码以获取网页中的图片。获取图片后再进行二维码识别、保存图片等操作。


具体实现:

1、给webview添加长按手势

 UILongPressGestureRecognizer* longPressed = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];

    longPressed.delegate = self;

 [self.webView addGestureRecognizer:longPressed];

2、实现长按方法, 在长按方法中注入js 获取图片(获取的时图片的URL, 就是image标签里的 src)

- (void)longPressed:(UILongPressGestureRecognizer*)recognizer

{

    if (recognizer.state != UIGestureRecognizerStateBegan) {

        return;

    }

    

    CGPoint touchPoint = [recognizer locationInView:self.webView];

    // 获取手势所在图片的URLjs中图片的地址是用src引用的

    NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];

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

    

    if (urlToSave.length == 0) {

        return;

    }

    

    [self showImageOptionsWithUrl:urlToSave];

}


3、实现showImageOptionsWithUrl方法, 通过url加载图片,进行二维码码识别,保存图片


- (void)showImageOptionsWithUrl:(NSString *)imgURL

{

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

    

    NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];

    UIImage* image = [UIImage imageWithData:data];

    

    NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:

                             @"CIDetectorAccuracy", @"CIDetectorAccuracyHigh",nil];

    CIDetector *detector = nil;

    //if (IOS8_Later)

    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]]){

            NSLog(@"scannedResult = %@", scannedResult);


        }else{

            NSLog(@"无法识别的网址");

            [SVProgressHUD showErrorWithStatus:@"无法识别的网址"];

        }

    }];

    

    // 保存图片到手机

    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];

    

}


4 、补充

// 功能:显示图片保存结果

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo

{

    if (error){

        NSLog(@"保存图片失败");

    }else {

        NSLog(@"保存图成功");


    }

}



以上,代码是偷来的,思想是自己总结的。。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值