2014-11-20 7 views
3

Я создаю скриншот отображаемого UIView. Я столкнулся с проблемой, когда элементы управления искажены и растянуты на мгновение (0,2-0,5 с) во время этого процесса. Он воспроизводится только для iPhone 6, 6+.Скриншот текущего UIView искажает и растягивает UIView на UIView.DrawViewHierarchy

мой простой код здесь (Xamarin.iOS):

public UIImage CreateScreenshotImage() 
{ 
    UIView rootView = UIApplication.SharedApplication.KeyWindow.RootViewController.View; 
    if (rootView != null) 
    { 
     var bounds = rootView.Bounds; 
     UIGraphics.BeginImageContextWithOptions(bounds.Size, false, 0); 
     **//here distortion starts, I can clearly see it in the DEBUG mode** 
     rootView.DrawViewHierarchy(bounds, true); 
     var screenshotImage = UIGraphics.GetImageFromCurrentImageContext(); 
     UIGraphics.EndImageContext(); 
     return screenshotImage; 
    } 
    return null; 
} 

Я не считаю, что это связано с Xamarin поэтому она должна быть такой же проблемой для родного приложения IOS, а также.

В чем может быть причина?

ответ

2

Try с этим:

public static class UIViewScreenshot 
{ 
    public static UIImage Screenshot(this UIView view, bool optimized = true) 
    { 
     if (optimized) 
     { 
      // take screenshot of the view 
      if (view.GetType().Name == "MKMapView") { 
       if (float.Parse(UIDevice.CurrentDevice.SystemVersion) >= 6.0) { 

        // in iOS6, there is no problem using a non-retina screenshot in a retina display screen 
        UIGraphics.BeginImageContextWithOptions (view.Frame.Size, false, 1.0f); 

       } else { 

        // if the view is a mapview in iOS5.0 and below, screenshot has to take the screen scale into consideration 
        // else, the screen shot in retina display devices will be of a less detail map (note, it is not the size of the screenshot, but it is the level of detail of the screenshot 
        UIGraphics.BeginImageContextWithOptions (view.Frame.Size, false, 1.0f); 
       } 
      } else { 
       // for performance consideration, everything else other than mapview will use a lower quality screenshot 
       UIGraphics.BeginImageContext (view.Frame.Size); 
      } 

     } else 
     { 
      UIGraphics.BeginImageContextWithOptions (view.Frame.Size, false, 0.0f); 
     } 


     var context = UIGraphics.GetCurrentContext(); 

     if (context == null) 
     { 
      Console.WriteLine("UIGraphicsGetCurrentContext() is nil. You may have a UIView with CGRectZero"); 
      return null; 
     } 
     else 
     { 
      view.Layer.RenderInContext (context); 

      var screenshot = UIGraphics.GetImageFromCurrentImageContext(); 
      UIGraphics.EndImageContext(); 

      return screenshot; 
     } 
    } 
} 
+0

Этот код работает для меня, но у меня есть вид камеры, которая не оказывает, когда 'view.Layer.RenderInContext()' называется. Он отображается только с помощью 'view.DrawViewHierarchy'. Есть идеи? – vrwim