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(@"保存图成功");


    }

}



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



你可能感兴趣的:(ios APPweb网页长按识别二维码和保存图片)