当前UIView的屏幕截图扭曲并拉伸UIView.DrawViewHierarchy中的UIView

当前UIView的屏幕截图扭曲并拉伸UIView.DrawViewHierarchy中的UIView,uiview,xamarin.ios,xamarin,screenshot,uigraphicscontext,Uiview,Xamarin.ios,Xamarin,Screenshot,Uigraphicscontext,我正在创建当前显示的UIView的屏幕截图。 在这个过程中,我遇到了一个问题:控件在一瞬间(0.2-0.5秒)被扭曲和拉伸。 它仅适用于iPhone 6,6+版本 我的简单代码如下(Xamarin.iOS): 我不相信它与Xamarin有关,所以对于本机iOS应用程序来说也应该是同一个问题 原因可能是什么?试试这个: public static class UIViewScreenshot { public static UIImage Screenshot(this UIView vi

我正在创建当前显示的UIView的屏幕截图。 在这个过程中,我遇到了一个问题:控件在一瞬间(0.2-0.5秒)被扭曲和拉伸。 它仅适用于iPhone 6,6+版本

我的简单代码如下(Xamarin.iOS):

我不相信它与Xamarin有关,所以对于本机iOS应用程序来说也应该是同一个问题

原因可能是什么?

试试这个:

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

这段代码对我适用,但我有一个摄影机视图,当调用
view.Layer.RenderInContext()
时,它不会渲染。它仅使用
view.DrawViewHierarchy
进行渲染。有什么想法吗?
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;
        }
    }
}