Печать экрана UIWebView - PullRequest
1 голос
/ 11 июня 2011

У меня есть UIWebView с графикой, сгенерированной Google Charts (представлен javascript)

Экран не pdf или png.

Можно ли захватить то, что на экране, и отправитьэто в AirPrint?

Ответы [ 3 ]

5 голосов
/ 11 июня 2011

Сделайте скриншот веб-просмотра и отправьте его в 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();
3 голосов
/ 12 июня 2011

Вот ответ, переведенный на 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;
    }
}
0 голосов
/ 18 ноября 2013

Функция '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;
    }
...