UIWebView擷取所點位置圖片URL教程是本文要介紹的內容,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];
- 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);
- }
- }
小結:詳解使用UIWebView擷取所點位置圖片URL教程的內容介紹完了,希望本文對你有所協助!