在我的应用程序中,我有一个webview,并希望打开一个单独的视图控制器中的网页中加载的图像,这很好,我需要做的就是获取图像来源的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打开链接(只有图像中的链接)(如果存在)。有关我如何实现这一目标的任何指示?
答案 0 :(得分:2)
最后想出来了,答案在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;
}
}