【问题标题】:Gray areas all over the web view整个 Web 视图中的灰色区域
【发布时间】:2014-06-29 20:52:28
【问题描述】:

我正在尝试保存整个 UIWebView 的屏幕截图。但是:如果网页很长,有很多 javascript 或动态对象,并且如果我不手动(并且耐心地!)滚动所有页面,webview 永远不会加载一些保持灰色和永远卸载的区域。 如何加载整个网页?或者这是一个不同的问题? 互联网上没有任何东西对我有帮助。 谢谢。

更新:

这是我目前在“PrintButton”IBAction 中所做的:

// save old frame of webView
    CGPoint savedContentOffset =    _webPage.scrollView.contentOffset;
    CGRect  savedFrame =            _webPage.scrollView.frame;

    CGSize  webPageSize =    CGSizeMake(_webPage.scrollView.contentSize.width, _webPage.scrollView.contentSize.height);
    CGRect  stringRect =     CGRectMake(0, 0, webPageSize.width, webPageSize.height);


    // set new frame
    _webPage.scrollView.contentOffset = CGPointZero;
    _webPage.scrollView.frame = stringRect;


    UIGraphicsBeginImageContextWithOptions(webPageSize,NO,1.0);


    [_webPage.scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
    self.viewImage = UIGraphicsGetImageFromCurrentImageContext();

    // re-set old frame
    _webPage.scrollView.contentOffset = savedContentOffset;
    _webPage.scrollView.frame = savedFrame;

    UIGraphicsEndImageContext();

    // save in gallery
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

代码有效:图像保存在我的照片库中。但是如前所述,如果网页很复杂,很长,很重,我不知道是什么,会出现空白区域(我说灰色是因为 webview 的背景是灰色的)。 通常,如果我在按下打印按钮之前不滚动页面,我未滚动的页面的 95% 是空白的,灰色的。

【问题讨论】:

  • 你想做什么??是快照问题还是开发问题?
  • 请更具体。你具体做了什么?贴一些代码
  • 我发布了一些代码并解释了我的问题。我希望很清楚,抱歉,英语不是我的母语。
  • 您是否可以加载页面,使其缩小以适应屏幕? (webView.scalesPageToFit = YES;) 加载后,然后以某种方式放大,然后截取屏幕截图?玩弄它,看看会发生什么。只是一个想法。祝你好运。
  • Guntis,我认为这是一个好主意(让所有网页同时可见,等到它加载然后重新缩放它),但我尝试了 scalesPageToFit 但它没有这样做...

标签: ios scrollview snapshot


【解决方案1】:

我已经尝试了几种解决这个问题的变体,并发现了 2 个关键的变化似乎使它工作得更好。

首先,我在_webView 上运行renderInContext 而不是_webView.scrollView。另一种是我通过调用scrollRectToVisible:animated:而不是手动设置contentOffset来调整内容的偏移量。

查看获取一系列图像并将其保存到相机胶卷的示例代码。通过一些修改,您可以将图像输出到单个高视图中,并将其保存为单个连续图像。

请注意,这假设网页只需要垂直滚动,而不是水平滚动。

- (void) printScreen {
    // this is a method that starts the screenshot taking process

    // this line is necessary to capture the completion of the scrollView animation
    _webView.scrollView.delegate = self;

    // save the first image
    UIGraphicsBeginImageContextWithOptions(_webView.bounds.size, NO, 0);
    [_webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

    // scroll to the next section
    [_webView.scrollView scrollRectToVisible:CGRectMake(0, _webView.frame.size.height, _webView.frame.size.width, _webView.frame.size.height) animated:YES];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    // at this point the webView scrolled to the next section

    // I save the offset to make the code a little easier to read
    CGFloat offset = _webView.scrollView.contentOffset.y;

    UIGraphicsBeginImageContextWithOptions(_webView.bounds.size, NO, 0);
    // note that the below line will not work if you replace _webView.layer with _webView.scrollView.layer
    [_webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

    // if we are not done yet, scroll to next section
    if (offset < _webView.scrollView.contentSize.height) {
        [_webView.scrollView scrollRectToVisible:CGRectMake(0, _webView.frame.size.height+offset, _webView.frame.size.width, _webView.frame.size.height) animated:YES];
    }
}

【讨论】:

  • 这很有帮助,谢谢。但是现在我有了所有的照片,我该如何把它们放在一起呢?我什么也没找到。
  • 如果您想要一个连续的图像,请创建一个与scrollView.contentSize 一样大的大视图,而不是将图像保存到相机胶卷中,而是将它们作为子视图添加到该容器中,并进行适当的更改到frame.origin。然后抓取该内容视图的“屏幕截图”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
相关资源
最近更新 更多