webview长按获取图片url地址
2016-06-07 14:54
696 查看
转载自: http://stackoverflow.com/questions/18239666/uiwebview-getting-image-url-with-elementfrompoint
- (void)longPressRecognized:(UILongPressGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { CGPoint point = [gestureRecognizer locationInView:webView]; longpressTouchedPoint = point; // convert point from view to HTML coordinate system CGSize viewSize = [webView frame].size; CGSize windowSize = [webView windowSize]; CGFloat f = windowSize.width / viewSize.width; if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 5.) { point.x = point.x * f; point.y = point.y * f; } else { // On iOS 4 and previous, document.elementFromPoint is not taking // offset into account, we have to handle it CGPoint offset = [webView scrollOffset]; point.x = point.x * f + offset.x; point.y = point.y * f + offset.y; } // Load the JavaScript code from the Resources and inject it into the web page NSString *path = [[NSBundle mainBundle] pathForResource:@"JSTools" ofType:@"js"]; NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; [webView stringByEvaluatingJavaScriptFromString: jsCode]; // call js functions NSString *tags = [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"getHTMLElementsAtPoint(%i,%i);",(NSInteger)point.x,(NSInteger)point.y]]; NSString *tagsSRC = [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"getLinkSRCAtPoint(%i,%i);",(NSInteger)point.x,(NSInteger)point.y]]; NSLog(@"src : %@",tags); NSLog(@"src : %@",tagsSRC); NSString *url = nil; if ([tags rangeOfString:@",IMG,"].location != NSNotFound) { url = tagsSRC; // Here is the image url! }
function getHTMLElementsAtPoint(x,y) { var tags = ""; var e; var offset = 0; while ((tags.search(",(A|IMG),") < 0) && (offset < 20)) { tags = ","; e = document.elementFromPoint(x,y+offset); while (e) { if (e.tagName) { tags += e.tagName + ','; } e = e.parentNode; } if (tags.search(",(A|IMG),") < 0) { e = document.elementFromPoint(x,y-offset); while (e) { if (e.tagName) { tags += e.tagName + ','; } e = e.parentNode; } } offset++; } return tags;} function getLinkSRCAtPoint(x,y) { var tags = ""; var e = ""; var offset = 0; while ((tags.length == 0) && (offset < 20)) { e = document.elementFromPoint(x,y+offset); while (e) { if (e.src) { tags += e.src; break; } e = e.parentNode; } if (tags.length == 0) { e = document.elementFromPoint(x,y-offset); while (e) { if (e.src) { tags += e.src; break; } e = e.parentNode; } } offset++; } return tags;}
相关文章推荐
- cocos2d 了解
- 抢鲜版 Android studio 2.2 pre 版本,千万不要想用
- Unity 5 事件/消息机制之UnityEvent类型
- android下properties文件的读写操作
- 微信分享时链接地址代替了描述的问题
- Android事件的分发(一)
- Android模拟器之Genymotion下载安装
- android学习笔记
- iOS 开发:绘制像素到屏幕
- Android_Sqlbrite入门使用
- Google VR SDK for Android(1)-introduction(介绍)
- iOS开发拓展篇-XMPP简单介绍
- iOS开发拓展篇—应用之间的跳转和数据传
- 使用CoordinatorLayout实现ToolBar扩展与收缩
- Android多线程研究(9)——线程锁Lock
- iOS数据持久化 归档 plist文件
- Android终止线程的方法
- CocoaPods的安装及第三方类库的使用
- ios开发之OC基础-ios开发学习路线图
- iOS UIWebView中Objective-C与Javascript的深层交互(一)