Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5415836
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: C/C++

2012-02-02 11:34:32

UIWebView有自己的UIResgure,如果我们手动加入自己的GestureRecognize将不能识别,如UILongPressGestureRecongnizer. 在浏览网页的时候,如果看到喜欢的图片,想把它保存下来如何办呢? 我们可以自己写一个程序来实现,用uiwebview开发一个自己的浏览器。

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

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

然后就是实现doubleTap:

-(void) doubleTap :(UITapGestureRecognizer*) sender
{
//
//
int scrollPositionY = [[self.theWebView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
int scrollPositionX = [[self.theWebView stringByEvaluatingJavaScriptFromString:@"window.pageXOffset"] intValue];

int displayWidth = [[self.theWebView stringByEvaluatingJavaScriptFromString:@"window.outerWidth"] intValue];
CGFloat scale = theWebView.frame.size.width / displayWidth;

CGPoint pt = [sender locationInView:self.theWebView];
pt.x *= scale;
pt.y *= scale;
pt.x += scrollPositionX;
pt.y += scrollPositionY;

NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", pt.x, pt.y];
NSString * tagName = [self.theWebView stringByEvaluatingJavaScriptFromString:js];
if ([tagName isEqualToString:@"img"]) {
NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];
NSString *urlToSave = [self.theWebView stringByEvaluatingJavaScriptFromString:imgURL];
NSLog(@"image url=%@", urlToSave);
}
}

这样我们就可以得到图片的url,然后下载保存就行了。
阅读(19076) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~