我正在创建当前显示的UIView的屏幕截图。 在此过程中,当控件扭曲并拉伸片刻(0.2-0.5秒)时,我遇到了一个问题。 它仅适用于iPhone 6,6 +。
我的简单代码在这里(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应用程序也应该是同样的问题。
可能是什么原因?
答案 0 :(得分:2)
试试这个:
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;
}
}
}