截取UIWebView中图像的链接

在我的应用程序中,我有一个webview,并希望打开加载在一个单独的视图控制器的网页中的图像,这是很好的,我所需要做的就是获取图像的源URL并加载在不同的视图控制器,我可以做。

这里是我用来获取图像的源代码的URL的代码。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { CGPoint touchPoint = [touch locationInView:self.view]; NSString *imageSRC = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y]; NSString *srcOfImage = [webView stringByEvaluatingJavaScriptFromString:imageSRC]; NSLog(@"src:%@",srcOfImage); } return YES; } 

现在,我的问题是,有时,当一个图像可能有一个链接(即标签),webview将加载链接,而图像打开在我单独的视图控制器。 我想要做的是,停止打开链接的webview(只有在图像中的),如果存在。 任何指向我可以如何去完成呢?

终于搞明白了,答案在于UIWebViewDelegate。 对于任何有兴趣的人来说,我是这样解决的

 bool isImage; -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { CGPoint touchPoint = [touch locationInView:self.view]; NSString *imageSRC = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y]; NSString *srcOfImage = [webView stringByEvaluatingJavaScriptFromString:imageSRC]; NSLog(@"src:%@",srcOfImage); NSURL *imgsrcURL = [NSURL URLWithString:srcOfImage]; if (imgsrcURL && imgsrcURL.scheme && imgsrcURL.host) { isImage = TRUE; } } return YES; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if ((navigationType == UIWebViewNavigationTypeLinkClicked) && (isImage)) { return NO; isImage = FALSE; } else { return YES; } }