18910140161

WKWebView获取H5页面里图片地址 图片相对视图窗口的坐标的方法

顺晟科技

2021-06-16 10:39:59

388

WKWebView获取H5页面里图片地址以及图片相对视图窗口的坐标的方法

最近有个需求是点击放大查看H5内容页面的图片,寻找到一个比较轻量的库YHPhotoBrowser,其中根据图片位置来缩放的动画需要传递一个图片所在屏幕里的位置CGRect,想获取H5的图片坐标那就需要射流研究…注入了

-(void)handleSingleTap:(UITapGestureRecognizer *)识别器{

CGPoint TouchPoint=[识别器位置查看器:自我。WebView];

' NSString * jsString=[NSString string with format : @ '函数getURLandRect(){\

var ele=文档。ElEMENTFromPoint(% f,% f);\

var url=ele.src\

var left=ele。getboundingclientrect().向左;\

var top=ele。getboundingclientrect().top\

var width=ele。getboundingclientrect().宽度;\

可变高度=ele。getboundingclientrect().身高;\

var JSonString=` { \ ' URL ' : \ ' $ { URL } \ ',\ ' left \ ' : \ ' $ { left } \ ',\ ' top \ ' : \ ' $ { top } \ ',\ ' width \ ' : \ ' $ { width } \ ',\ ' height ' : \ ' $ { height } \ ' } `;\

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

【自我。webview evaluatejavascript : jsstring completionhandler:^(id _ nullable result,NSError * _Nullable error) {

NSDictionary * resultDic=[小工具converttodictionary :(NSString *)结果];

NSString * imageURL=[小工具是NullToString : resultdic[@ ' URL '];

if(图像URl。length==0 | |[image URl isequaltostring : @ ' undefined ']){

返回;

}

CGFloat IMGx=[ResultDic[@ ' left ']float VaLue];

CGFloat IMgy=[resultDic[@ ' top ']float VaLue]NAV _ HEIGHT 5;

CGFloat IMgw=[ResultDic[@ ' width ']float VaLue];

CGFloat IMGH=[ResultDic[@ ' height ']float VaLue];

自我。PhotoView=[[PhotoBrowser alloc]init];

自我。PhotoView。SourceView=self。视图;//图片所在的父容器

自我。PhotoView。urlimgarr=@[图像URl];//网络链接图片的数组

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

自我。PhotoView。SourceRect=cgrectMake(IMGx,imgY,imgW,IMGH);

}

自我。PhotoView。索引标记=0;//初始化进去显示的图片下标

【自我。view addsubview : self。photo view];//叠加在当前副主席上

}];

}

这个射流研究…方法document.elementFromPoint(%f,%f)根据传入的点返回该点最上层的对象,通过科学研究委员会找到图片的链接,然后getBoundingClientRect()方法能返回对象的八个属性左,上,右,下,x,y,宽度,高度,根据需要获取相应属性构造原图的当需要获取一个即可

这里附带一下判断对象合法性以及数据字符串转NSDictionary的工具方法

(NSString *)IsNullToString :(id)字符串{

if([string isequal : @ ' NULL ']| |[string is kind of class :[NSNULL class]]|[string isequal :[NSNULL NULL]]| | | |[string isequal : Null]| |[[string class]issub class of class 3360[NSNULL class]]| | string==nil | | string==NULL | |[string Iskind of class :[NSNULL class]| |[[StringBytcriminal]

return @

} else {

return (NSString *)字符串;

}

}

(NSdictionary *)converttodictionary :(NSString *)jsonStr {

NSData * data=[jsonStr data using encoding : nsutf 8 string ncoding];

NSDictionary * TempDic=[nsjsonSerialization jsonobjectwithdata :数据选项33600错误: nil];

返回tempDic

}

相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航