iOS WKWebView识别H5中的图片资源

想获取H5的图片坐标那就需要JS注入了

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {

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

    NSString *jsString = [NSString stringWithFormat:@"function getURLandRect(){\

                            var ele=document.elementFromPoint(%f, %f);\

                            var url=ele.src;\

                            var left=ele.getBoundingClientRect().left;\

                            var top=ele.getBoundingClientRect().top;\

                            var width=ele.getBoundingClientRect().width;\

                            var height=ele.getBoundingClientRect().height;\

                            var jsonString= `{\"url\":\"${url}\",\"left\":\"${left}\",\"top\":\"${top}\",\"width\":\"${width}\",\"height\":\"${height}\"}`;\

                            return(jsonString)} getURLandRect()", touchPoint.x, touchPoint.y];


    [self.webView evaluateJavaScript:jsString completionHandler:^(id _Nullable result, NSError * _Nullable error) {

        NSDictionary *resultDic = [SmallTools convertToDictionary:(NSString *)result];

        NSString *imageURL = [SmallTools isNullToString:resultDic[@"url"]];

        if (imageURL.length == 0 || [imageURL isEqualToString:@"undefined"]) {

            return;

        }

        CGFloat imgX = [resultDic[@"left"] floatValue];

        CGFloat imgY = [resultDic[@"top"] floatValue] + NAV_HEIGHT + 5;

        CGFloat imgW = [resultDic[@"width"] floatValue];

        CGFloat imgH = [resultDic[@"height"] floatValue];


        self.photoView = [[YHPhotoBrowser alloc]init];

        self.photoView.sourceView = self.view; //图片所在的父容器

        self.photoView.urlImgArr = @[imageURL]; //网络链接图片的数组

        if (imgX && imgY && imgW && imgH) { //原图片所在屏幕位置

            self.photoView.sourceRect = CGRectMake(imgX, imgY, imgW, imgH);

        }

        self.photoView.indexTag = 0; //初始化进去显示的图片下标

        [self.view addSubview:self.photoView]; //叠加在当前VC上

    }];

}

这个JS方法document.elementFromPoint(%f, %f)根据传入的点返回该点最上层的对象,通过src找到图片的链接,然后getBoundingClientRect()方法能返回对象的八个属性left, top, right, bottom, x, y, width, height,根据需要获取相应属性构造原图的CGRect即可

这里附带一下判断NSString合法性以及JSON字符串转NSDictionary的工具方法+ (NSString *)isNullToString:(id)string { if ([string isEqual:@"NULL"] || [string isKindOfClass:[NSNull class]] || [string isEqual:[NSNull null]] || [string isEqual:NULL] || [[string class] isSubclassOfClass:[NSNull class]] || string == nil || string == NULL || [string isKindOfClass:[NSNull class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string isEqualToString:@""] || [string isEqualToString:@"(null)"]) { return @""; } else { return (NSString *)string; }}

+ (NSDictionary *)convertToDictionary:(NSString *)jsonStr {

    NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *tempDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    return tempDic;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。