UIWebView之获取所点位置图片URL
2014-05-27 17:17
197 查看
UIWebView有自己的UIResgure,如果我们手动加入自己的GestureRecognize将不能识别,如UILongPressGestureRecongnizer. 在浏览网页的时候,如果看到喜欢的图片,想把它保存下来如何办呢?
我们可以自己写一个程序来实现,用uiwebview开发一个自己的浏览器。
![](http://www.cocoachina.com/cms/uploads/allimg/111031/3292_111031153011_1.jpg)
关面说到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
{
// <Find HTML tag which was clicked by user>
// <If tag is IMG, then get image URL and start saving>
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,然后下载保存就行了。
转载:http://www.cocoachina.com/bbs/read.php?tid=73570&page=20
我们可以自己写一个程序来实现,用uiwebview开发一个自己的浏览器。
![](http://www.cocoachina.com/cms/uploads/allimg/111031/3292_111031153011_1.jpg)
关面说到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
{
// <Find HTML tag which was clicked by user>
// <If tag is IMG, then get image URL and start saving>
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,然后下载保存就行了。
转载:http://www.cocoachina.com/bbs/read.php?tid=73570&page=20
相关文章推荐
- UIWebView之获取所点位置图片URL
- UIWebView之获取所点位置图片URL
- UIWebView之获取所点位置图片URL
- UIWebView之获取所点位置图片URL
- 详解使用UIWebView获取所点位置图片URL教程
- UIWebView之获取所点位置图片URL
- UIWebView之获取所点位置图片URL
- UIWebView之获取所点位置图片URL
- UIWebView获取所点位置图片URL
- UIWebView之获取所点位置图片URL
- UIWebView之获取所点位置图片URL
- UIWebView之获取所点位置图片URL
- UIWebView之获取所点位置图片URL
- UIWebView之获取所点位置的图片(放大webView中的图片)
- 点击UIWebView获取图片的URL的两种方法
- 获取 UIWebView中用户所点击的图片URL
- 【转】UIWebView获取当前页面url的两种方法
- 如何获取iPhone拍摄的png照片的拍摄时间,位置信息等图片属性?
- 转:通过 POI 获取图片在 Excel 表格中的位置
- Android获取SD卡中选中图片的路径(URL)