详解使用UIWebView获取所点位置图片URL教程

移动开发 iOS
本文介绍的是使用UIWebView获取所点位置图片URL教程,主要介绍了UIWebView获取图片URL的内容,先来看详细内容。

UIWebView获取所点位置图片URL教程是本文要介绍的内容,UIWebView有自己的UIResgure,如果我们手动加入自己的GestureRecognize将不能识别,如UILongPressGestureRecongnizer. 在浏览网页的时候,如果看到喜欢的图片,想把它保存下来如何办呢? 我们可以自己写一个程序来实现,用uiwebview开发一个自己的浏览器。

关于说到uiwebview不能识别long press gesture,幸好有一个可以识别,那就是double click.因此我们注册它,代码如下:

  1. UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];    
  2. doubleTap.numberOfTouchesRequired = 2;    
  3. [self.theWebView addGestureRecognizer:doubleTap];    
  4.  UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];  
  5.  doubleTap.numberOfTouchesRequired = 2;  
  6.  [self.theWebView addGestureRecognizer:doubleTap]; 

然后就是实现doubleTap:

  1. -(void) doubleTap :(UITapGestureRecognizer*) sender    
  2. {    
  3. //  <Find HTML tag which was clicked by user>     
  4. //  <If tag is IMG, then get image URL and start saving>     
  5.     int scrollPositionY = [[self.theWebView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];    
  6.     int scrollPositionX = [[self.theWebView stringByEvaluatingJavaScriptFromString:@"window.pageXOffset"] intValue];    
  7.         
  8.     int displayWidth = [[self.theWebView stringByEvaluatingJavaScriptFromString:@"window.outerWidth"] intValue];    
  9.     CGFloat scale = theWebView.frame.size.width / displayWidth;    
  10.         
  11.     CGPoint pt = [sender locationInView:self.theWebView];    
  12.     pt.x *= scale;    
  13.     pt.y *= scale;    
  14.     pt.x += scrollPositionX;    
  15.     pt.y += scrollPositionY;    
  16.         
  17.     NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", pt.x, pt.y];    
  18.     NSString * tagName = [self.theWebView stringByEvaluatingJavaScriptFromString:js];    
  19.     if ([tagName isEqualToString:@"img"]) {    
  20.         NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];    
  21.         NSString *urlToSave = [self.theWebView stringByEvaluatingJavaScriptFromString:imgURL];    
  22.         NSLog(@"image url=%@", urlToSave);    
  23.     }    
  24. }   

小结:详解使用UIWebView获取所点位置图片URL教程的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2015-06-26 09:59:19

UIWebViewiOSURL

2011-08-02 11:07:42

iOS开发 UIWebView

2011-07-21 17:00:59

iPhone UIWebView Status Cod

2011-08-08 17:59:22

UIWebView网络网页

2021-08-30 09:58:52

鸿蒙HarmonyOS应用

2011-08-01 09:50:46

iPhone 获取对象 UIView

2010-09-30 15:24:31

滚动条Javascript

2010-05-21 12:58:01

Subversion使

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2019-11-21 11:04:23

企业上云云计算

2010-05-24 11:16:43

SVN更新URL

2009-07-09 17:58:21

MyEclipse常用

2013-04-22 13:51:08

Android开发Android中App

2015-08-17 09:46:15

UIjs

2023-11-30 08:06:43

Springboot地理位置

2023-10-27 16:12:29

2013-04-17 09:08:23

Windows 8.1

2013-12-06 10:12:49

Android游戏引擎libgdx教程

2009-11-23 15:32:13

PHP获取远程URL

2010-10-09 11:01:31

JS
点赞
收藏

51CTO技术栈公众号