【问题标题】:Convert UIWebview contents to a UIImage when the webview is larger than the screen当 webview 大于屏幕时,将 UIWebview 内容转换为 UIImage
【发布时间】:2011-05-27 09:35:33
【问题描述】:

this question(以及this answer)非常相似,我正在尝试从webview 中制作一个UIImage。到目前为止,我正在使用答案中建议的代码,特别是:

UIGraphicsBeginImageContext(webview.bounds.size);
[webview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但是,当 webview 的内容大于屏幕时,生成的图像不完整并且缺少补丁。有关如何解决此问题的任何想法?

【问题讨论】:

    标签: iphone objective-c ios uiwebview uiimage


    【解决方案1】:

    如果有人在 Xamarin 中苦苦挣扎,根据@DR 的回答,这里是代码:

    var image = new UIImage();
    
    var previousFrame = _tableView.Frame;
    var newFrame = new CGRect(new CGPoint(0, 0), _tableView.ContentSize);
    View.Frame = newFrame;
    
    UIGraphics.BeginImageContextWithOptions(_tableView.ContentSize, false, UIScreen.MainScreen.Scale);
    var context = UIGraphics.GetCurrentContext();
    View.Layer.RenderInContext(context);
    image = UIGraphics.GetImageFromCurrentImageContext();
    
    UIGraphics.EndImageContext();
    
    View.Frame = previousFrame;
    
    
    return image;
    

    【讨论】:

      【解决方案2】:

      @Jibeex 的回答对我有用。但我不得不添加以下代码

          fullSizeFrame.size.width = self.scrollView.contentSize.width
      

      下面

          fullSizeFrame.size.height = self.scrollView.contentSize.height
      

      让它充分发挥作用。写作为答案,因为我没有足够的声誉来发表评论。

      【讨论】:

      • 为什么不fullSizeFrame.size = self.scrollView.contentSize
      【解决方案3】:

      D R 的回答很好。唯一缺少的是考虑到屏幕的规模。我在我的 Swift 版本中对其进行了更正,它对我来说很好用。

      extension UIWebView{
      func snapshotForCompletePage() -> UIImage {
          // tempframe to reset view size after image was created
          let tmpFrame: CGRect = self.frame
          // set full size Frame
          var fullSizeFrame: CGRect = self.frame
          fullSizeFrame.size.height = self.scrollView.contentSize.height
          self.frame = fullSizeFrame
      
          // here the image magic begins
          UIGraphicsBeginImageContextWithOptions(fullSizeFrame.size, false, UIScreen.mainScreen().scale)
          let resizedContext: CGContextRef = UIGraphicsGetCurrentContext()!
          self.layer.renderInContext(resizedContext)
          let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()
          // reset Frame of view to origin
          self.frame = tmpFrame
          return image
            }
          }
      

      【讨论】:

        【解决方案4】:

        我认为这会有所帮助

        UIGraphicsBeginImageContext([webView sizeThatFits:[[UIScreen mainScreen] bounds].size]]);
        

        【讨论】:

          【解决方案5】:

          我得到了答案:

          您必须在创建图像之前将 Webview 的框架设置为完整的内容大小。在此之后只需将大小设置回原点:

          + (UIImage *) imageFromWebView:(UIWebView *)view
          {
              // tempframe to reset view size after image was created
              CGRect tmpFrame         = view.frame;
          
              // set new Frame
              CGRect aFrame               = view.frame;
              aFrame.size.height  = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height;
              view.frame              = aFrame;
          
              // do image magic
              UIGraphicsBeginImageContext([view sizeThatFits:[[UIScreen mainScreen] bounds].size]);
          
              CGContextRef resizedContext = UIGraphicsGetCurrentContext();
              [view.layer renderInContext:resizedContext];
              UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
          
              UIGraphicsEndImageContext();
          
              // reset Frame of view to origin
              view.frame = tmpFrame;
              return image;
          }
          

          【讨论】:

          • 我非常喜欢人们在得到答案后发布答案。这就是流动的方式! :)
          • 顺便说一句,我只是想知道 - 为什么这是一个单例而不只是一个函数?作为一个仍然不使用单例的人,我出于好奇而问
          • 我开始明白为什么它不能与'-'一起使用,但我如何使用'+'?
          • Re“Hamutsi”:我所做的是为 UIImage 添加一个“类别”并为其声明此方法。
          • 我使用此功能,它在 iOS 4.3 中运行良好,但是当我在 iOS 5 中运行相同的代码行时,它停止工作,任何人都可以在这里帮助我:(
          猜你喜欢
          • 2010-10-25
          • 2017-10-23
          • 1970-01-01
          • 2012-01-03
          • 1970-01-01
          • 2014-09-19
          • 2021-12-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多