【问题标题】:Print UIWebView screen打印 UIWebView 屏幕
【发布时间】:2011-06-11 00:27:49
【问题描述】:

我有一个 UIWebView,其中包含由 Google Charts 生成的图形(javascript 渲染)

屏幕不是 pdf 或 png。

是否可以捕获屏幕上的内容并将其发送到 AirPrint?

【问题讨论】:

  • 你能截个图发过来吗?

标签: iphone ios ipad uiwebview airprint


【解决方案1】:

截取网页视图并将其发送到 AirPrint。

// TAKE SCREENSHOT
CGRect myRect = [self.view bounds];
UIGraphicsBeginImageContext(myRect.size);   
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, myRect);
[self.view.layer renderInContext:ctx];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(viewImage, 1.0);
UIGraphicsEndImageContext();

【讨论】:

  • +1 对于 PengOne,这是打印屏幕,获取屏幕截图的正确方法。我使用相同的方法。好吧 ivink 不使用你说这是不可能的。你得到了答案执行那个亲爱的
【解决方案2】:

这是翻译成 Monotouch .NET c# 的答案

  public static UIImage TakeScreenShot (UIView view)
{
    try
    {
        RectangleF canvasRect = view.Bounds;
        UIGraphics.BeginImageContext (canvasRect.Size);

        CGContext ctx = UIGraphics.GetCurrentContext ();
        ctx.FillRect (canvasRect);
        view.Layer.RenderInContext (ctx);

        UIImage newImage = UIGraphics.GetImageFromCurrentImageContext ();

        UIGraphics.EndImageContext ();

        return newImage;
    }
    catch
    {
        return null;
    }
}

【讨论】:

    【解决方案3】:

    UIGetWebViewPrintScreen 函数应该在 UIWebView.LoadFinished 之后调用

    void main()
    {
      RectangleF rect(0, 0, 100, 100);
      UIWebView = new UIWebView();
      UIImage Img = null;
      string sHtml = "test string";
      WebView.LoadHtmlString(sHtml, null);
      WebView.Frame = rect;
      WebView.LoadFinished += delegate
      {
        if (UIGetWebViewPrintScreen (WebVIew, Img))
        {
          //doing with Img what you want.
        }
      }
    }
    
    
    public bool UIGetWebViewPrintScreen(UIWebView WebView, out UIImage Img)
        {
          bool bSuccess = false;
          //Should be call after event uiWebView.LoadFinished
          UIGraphics.BeginImageContext(WebView.ScrollView.Bounds.Size);
          CGContext cnt = UIGraphics.GetCurrentContext();
          if (cnt != null)
          {
            WebView.Layer.RenderInContext(cnt);
            Img = UIGraphics.GetImageFromCurrentImageContext();
            bSuccess = true;
          }
          else
            Img = null;
          UIGraphics.EndImageContext();
    
          return bSuccess;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2014-12-06
      相关资源
      最近更新 更多