2012-03-14 70 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