美文网首页iOS点点滴滴iOS开发记录
WKWebView长按保存图片逻辑

WKWebView长按保存图片逻辑

作者: freesan44 | 来源:发表于2020-05-08 17:07 被阅读0次

    问题

    在WKWebView上的图片,长按会触发系统控件进行保存和共享,但通过系统共享是没法分享图片,只能进行长按手势覆盖,然后用本地控件实现

    解决方案

    1. 屏蔽WebKit的长按逻辑
     //禁止长按逻辑
        NSMutableString *javascript = [NSMutableString string];
        [javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];//禁止长按
        WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        [config.userContentController addUserScript:noneSelectScript];
    
    1. 添加长按手势
    -(void)addLongPressGesture
    {
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(startLongPress:)];
        longPress.delegate = self;
    
        longPress.minimumPressDuration = 0.4f;
        longPress.numberOfTouchesRequired = 1;
        longPress.cancelsTouchesInView = YES;
        [self.locationWebView addGestureRecognizer:longPress];
        
    }
    - (void)startLongPress:(UILongPressGestureRecognizer *)pressSender
    {
        if(pressSender.state == UIGestureRecognizerStateBegan){
    
           //实现相关功能
            [self detectInWebView:pressSender];
            DebugLog(@"1. 开始长按手势");
    
        }else if(pressSender.state == UIGestureRecognizerStateEnded){
    
            //可以添加你长按手势执行的方法,不过是在手指松开后执行
            DebugLog(@"2. 结束长按手势");
    
        }else if(pressSender.state == UIGestureRecognizerStateChanged){
    
            //在手指点下去一直不松开的状态执行
            DebugLog(@"3. 长按手势改变");
        }
    }
    
    1. 手势中捕捉图片进行处理
    - (void)detectInWebView:(UIGestureRecognizer *)ges{
    
        CGPoint touchPoint = [ges locationInView:ges.view];
        NSString *jsString = [NSString stringWithFormat:@"function getURLandRect(){\
                                  var ele=document.elementFromPoint(%f, %f);\
                                  var url=ele.src;\
                                  var jsonString= `{\"url\":\"${url}\"}`;\
                                  return(jsonString)} getURLandRect()", touchPoint.x, touchPoint.y];
        [self.locationWebView evaluateJavaScript:jsString completionHandler:^(id _Nullable result, NSError * _Nullable error) {
            NSData *data = [result dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary*resultDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSString* imageURL = resultDic[@"url"];
            if(imageURL.length==0|| [imageURL isEqualToString:@"undefined"]) {
                return;
            }
            NSData* imageData=nil;
            if(([imageURL hasPrefix:@"http"])) {
                imageData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]] returningResponse:NULL error:NULL];
                
            }else{
                NSString*dataString = [[imageURL componentsSeparatedByString:@","]lastObject];
    //            imageData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
                imageData = [[NSData alloc] initWithBase64EncodedString:dataString options:NSDataBase64DecodingIgnoreUnknownCharacters];
                
            }
            UIImage*image=[UIImage imageWithData:imageData];
            if(image) {
                //弹框处理
                [self.webModel popoverImageAlertWithImage:image Controller:self];
            }
        }];
    }
    

    相关文章

      网友评论

        本文标题:WKWebView长按保存图片逻辑

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