【问题标题】:taking "screenshot" of a variable height webview in iOS在 iOS 中获取可变高度 webview 的“截图”
【发布时间】:2013-10-16 12:37:20
【问题描述】:

谁能告诉我一些示例代码来捕获嵌入在 iphone 应用程序中的完整 web 视图的图像? webview 的高度取决于内容,因此简单的屏幕截图不会对用户有用,但我想在导航栏中为他们提供一个按钮,用于捕获全高图像并将其保存到他们的照片图书馆。

提前致谢!

【问题讨论】:

  • 你能找到Webview的宽高吗?
  • webview 有一个页面长度属性和一个页面计数属性,也许你可以使用这些值来调整你的 uiwebview 的大小,然后对调整大小的 webviews 层进行屏幕截图,然后重置原始宽度和高度。

标签: ios uiwebview webview screenshot


【解决方案1】:

由于UIWebViewUIScrollView上呈现内容,您可以通过更改UIScrollViewcontentOffset参数并加入屏幕截图,从上到下开始截图。并且,记得查看contentSize。祝你好运!

【讨论】:

    【解决方案2】:

    使用此代码:

        - (IBAction)buttonPressed:(id)sender 
    {
        webViewHeight = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];
    
    CGRect screenRect = self.myWebView.frame;
    
    double currentWebViewHeight = webViewHeight;
    while (currentWebViewHeight > 0)
    {
        imageName ++;
    
        UIGraphicsBeginImageContext(screenRect.size);
    
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [[UIColor blackColor] set];
        CGContextFillRect(ctx, screenRect);
    
        [self.myWebView.layer renderInContext:ctx];
    
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]];
    
        if(currentWebViewHeight < 960)
        {
            CGRect lastImageRect = CGRectMake(0, 960 - currentWebViewHeight, self.myWebView.frame.size.width, currentWebViewHeight);
            CGImageRef lastImageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);                
            newImage = [UIImage imageWithCGImage:lastImageRef]; 
            CGImageRelease(lastImageRef);
        }
    
        [UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];
    
        [self.myWebView stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,960);"];
        currentWebViewHeight -= 960;
     }
     }
    

    【讨论】:

      猜你喜欢
      • 2015-11-15
      • 1970-01-01
      • 2015-06-10
      • 2017-10-18
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 2014-06-01
      相关资源
      最近更新 更多