title: Converting touch points in UIWebView to save content
body: <p>I've been trying to save an image from a <code>UIWebView</code> when you press and hold over a visible element that happens to be an image. I'm struggling to find out how to convert the <code>UILongPressGestureRecognizer</code>'s touch points into the points needed for the Javascript to get the correct position. I'm thinking it has something to do with the webView's scrollView's content inset, but I'm not quite sure.</p>
  
  <p>I'd appreciate any help offered.</p>
  
  <p>Code:</p>
  
  <pre><code>- (void)longPress:(UILongPressGestureRecognizer *)sender
  {
      if (sender.state == UIGestureRecognizerStateBegan)
      {
          CGPoint pt = [sender locationInView:self.webView];
  
          CGPoint offset;
          offset.x = [[self.webView stringByEvaluatingJavaScriptFromString:@"window.pageXOffset"]integerValue];
          offset.y = [[self.webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"]integerValue];
  
          CGSize windowSize;
  
          windowSize.width = [[self.webView stringByEvaluatingJavaScriptFromString:@"window.innerWidth"]integerValue];
          windowSize.height = [[self.webView stringByEvaluatingJavaScriptFromString:@"window.innerHeight"]integerValue];
  
          CGFloat f = windowSize.width / self.webView.frame.size.width;
          pt.x = (pt.x * f);
          pt.y = (pt.y - self.webView.scrollView.contentInset.top) * f;
  
          NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", pt.x, pt.y];
          NSString *tagName = [self.webView stringByEvaluatingJavaScriptFromString:js];
  
          if (tagName &amp;&amp; [tagName caseInsensitiveCompare:@"IMG"] == NSOrderedSame)
          {
              NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];
              NSString *urlToDownload = [self.webView stringByEvaluatingJavaScriptFromString:imgURL];
  
              NSArray *validImageTypes = @[@".jpg",@".jpeg",@".gif",@".tif",@".tiff",@".ico",@".bmp",@".bmpf",@".cur",@".xbm",@".png"];
  
              for (NSString *sub in validImageTypes)
              {
                  if (urlToDownload.length &gt;= sub.length)
                  {
                      NSString *extension = [urlToDownload substringFromIndex:MAX(urlToDownload.length - sub.length, 0)];
  
                      if ([extension caseInsensitiveCompare:sub] == NSOrderedSame)
                      {
                          // Image found, proceed to download (code omitted)
                          break;
                      }
                  }
              }
          }
      }
  }
  </code></pre>