美文网首页webView与APP
iOS 获取UIWebView的H5页面中的图片地址

iOS 获取UIWebView的H5页面中的图片地址

作者: Cary9396 | 来源:发表于2018-11-19 16:16 被阅读0次

    现在许多APP都有H5网页,那么如果我们需要得到H5中的某些图片予以保存、收藏或者识别二维码等操作应当如何完成呢 ,以UIWebView为例:
    首先,创建一个UIWebView显示网页:

        _webView  = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        _webView.scalesPageToFit =YES;
        _webView.backgroundColor = [UIColor clearColor];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://weibo.com"]]];
        [self.view addSubview:_webView];
    

    给UIWebView加上长按手势:

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

    实现delegate方法:

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    

    实现手势事件方法:

    - (void)longPressed:(UITapGestureRecognizer*)recognizer
    {
        
       if (recognizer.state != UIGestureRecognizerStateBegan) {
            return;
        }
        CGPoint touchPoint = [recognizer locationInView:self.webView];
        NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
        NSString *imageUrl = [self.webView stringByEvaluatingJavaScriptFromString:js];
        if (imageUrl.length == 0) {
            return;
        }
        NSLog(@"image url:%@",imageUrl);
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
    
        UIImage *image = [UIImage imageWithData:data];
        if (image) {
    
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //保存图片操作
            }];
            UIAlertAction *CollectionAction = [UIAlertAction actionWithTitle:@"识别图中二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //识别二维码操作
            }];
            
            [alert addAction:cancelAction];
            [alert addAction:saveAction];
            [alert addAction:CollectionAction];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    }
    

    自此已拿到H5页面中你所需要的图片地址,可进行你需要的后续操作。

    相关文章

      网友评论

        本文标题:iOS 获取UIWebView的H5页面中的图片地址

        本文链接:https://www.haomeiwen.com/subject/cwqafqtx.html