2012-03-14 36 views
4

这里我加载了webview中的内容以及图像,我想从webview中获取图像并将其加载到imageview中。如何从ios中的uiwebview中获取图像

图像的URL被接收,但图像没有加载它到ImageView的

在这里,我使用的代码从web视图拍摄图像是

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
    //Touch gestures below top bar should not make the page turn. 
    //EDITED Check for only Tap here instead. 
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { 
     CGPoint touchPoint = [touch locationInView:self.view]; 

     NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 
     bool pageFlag = [userDefaults boolForKey:@"pageDirectionRTLFlag"]; 
     NSLog(@"pageFlag tapbtnRight %d", pageFlag); 

     if(self.interfaceOrientation==UIInterfaceOrientationPortrait||self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) { 
    NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y]; 
      NSString *urlToSave = [webView stringByEvaluatingJavaScriptFromString:imgURL]; 
      NSLog(@"urlToSave :%@",urlToSave); 
} 
} 

回答

16

检查了这一点。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
NSLog(@"TAPPED"); 
//Touch gestures below top bar should not make the page turn. 
//EDITED Check for only Tap here instead. 
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { 
    CGPoint touchPoint = [touch locationInView:self.view]; 

    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 
    bool pageFlag = [userDefaults boolForKey:@"pageDirectionRTLFlag"]; 
    NSLog(@"pageFlag tapbtnRight %d", pageFlag); 

    if(self.interfaceOrientation==UIInterfaceOrientationPortrait||self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) { 
     NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y]; 
     NSString *urlToSave = [webV stringByEvaluatingJavaScriptFromString:imgURL]; 
     NSLog(@"urlToSave :%@",urlToSave); 
     NSURL * imageURL = [NSURL URLWithString:urlToSave]; 
     NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 
     UIImage * image = [UIImage imageWithData:imageData]; 
     imgView.image = image;//imgView is the reference of UIImageView 
    } 
    } 
return YES; 
} 

示例代码: http://dl.dropbox.com/u/51367042/ImageFromWebView.zip

+0

似乎并没有在谷歌图片 – jjxtra 2013-10-26 15:08:02

+0

@PsychoDad你弄清楚如何使之成为谷歌的图像携手? – Crashalot 2014-07-26 01:07:49

+0

我告诉用户点击视图图像按钮。从那里它工作正常。 – jjxtra 2014-07-26 14:59:38