objective-c - 如何从ios中的uiwebview获取图像

标签 objective-c ios uiwebview uiimageview nsurl

这里我在 webview 中加载了内容和图像,我想从 webview 中获取图像并将其加载到 imageview 中。

接收到图片的url,但图片没有加载到imageview中

这里我使用从 webview 获取图像的代码是

-(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);
}
}

最佳答案

检查一下。

-(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

关于objective-c - 如何从ios中的uiwebview获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9703071/

相关文章:

objective-c - CGContext 的静态背景

iOS - Admob 内存泄漏

ios - xcode 发送 bool 值到soap webservice

ios - 如何获取屏幕的结束CGPoint?

ios - AFNetworking 2.0 多部分请求正文空白

ios - 如何在 iOS 警报中显示表格

ios - UIView 以编程方式设置约束

ios - 在 Xcode 的 UIWebView 中更改内容背景颜色

ios - 将 UIWebView 的(通过 Web 服务加载)<img> 标记源替换为本地(缓存)源

ios - 获取 UIWebView 不可见部分的图像