在UIWebView中截取图像中的链接 [英] Intercepting links in images in UIWebView
问题描述
在我的应用程序中,我有一个webview,并希望打开在一个单独的视图控制器中的网页中加载的图像,这是好的,我需要做的是获取的图像源的URL,加载它在一个不同的视图控制器,我可以做。
In my app, I have a webview, and wanted to open images loaded in a webpage in a separate view controller, which is fine, all I need to do is get the URL to the source of the image & load it in a different view controller, which I can do.
这是我使用的代码来获取图像的源。
Here is the code I'm using to get the URL to the source of the image.
-(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;
}
现在,我的问题是,有时,当一个图像可能有一个链接ie标签),webview将加载链接,而图像在我的单独的视图控制器中打开。我想做的是,停止webview打开链接(只有图像中的)如果存在。
Now, my question is, sometimes, when an image might have a link (i.e. tag) with it, the webview will load the link while the image opens in my separate view controller. What I would like to do is, stop the webview from opening the link (only the ones in the images) if one exists. Any pointers to how I may go about accomplishing that?
推荐答案
最后想出来,UIWebViewDelegate中的答案。对任何有兴趣的人,这是我如何解决它..
Finally figured it out, the answer lied in UIWebViewDelegate. For anyone who's interested, here's how I solved it..
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;
}
}
这篇关于在UIWebView中截取图像中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!