打印 UIWebView 屏幕

发布于 2024-11-15 02:13:05 字数 126 浏览 5 评论 0原文

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

屏幕不是 pdf 或 png。

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

I have a UIWebView with a graphic generated by Google Charts (javascript rendered)

The screen is not a pdf or png.

Is it possible to capture what's on the screen and send it to AirPrint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-11-22 02:13:05

截取网络视图的屏幕截图并将其发送到 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();

Take a screenshot of the webview and send it to 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();
刘备忘录 2024-11-22 02:13:05

这是翻译为 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;
    }
}

Here's the answer translated to 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;
    }
}
迷爱 2024-11-22 02:13:05

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

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

The function 'UIGetWebViewPrintScreen' should be called after 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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文