From 7f8c081789f334e88bca8f7413fc4a0f7503a6d6 Mon Sep 17 00:00:00 2001 From: Christian Helle Date: Wed, 23 Nov 2016 23:44:01 +0100 Subject: [PATCH 1/2] Use DrawViewHierarchy to capture a snapshot of the current screen --- src/UIKit/UIScreen.cs | 50 +++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/UIKit/UIScreen.cs b/src/UIKit/UIScreen.cs index c941673a3f2a..fd134310c7bc 100644 --- a/src/UIKit/UIScreen.cs +++ b/src/UIKit/UIScreen.cs @@ -37,31 +37,43 @@ public XamCore.CoreAnimation.CADisplayLink CreateDisplayLink (NSAction action) public UIImage Capture () { - // This is from: https://developer.apple.com/library/ios/#qa/qa2010/qa1703.html - var selScreen = new Selector ("screen"); - var size = Bounds.Size; + if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0)) { + // This is from https://developer.apple.com/library/content/qa/qa1817/_index.html + try { + var view = UIApplication.SharedApplication.KeyWindow; + UIGraphics.BeginImageContextWithOptions(view.Bounds.Size, view.Opaque, 0); + view.DrawViewHierarchy(view.Bounds, true); + return UIGraphics.GetImageFromCurrentImageContext(); + } finally { + UIGraphics.EndImageContext(); + } + } else { + // This is from: https://developer.apple.com/library/ios/#qa/qa2010/qa1703.html + var selScreen = new Selector("screen"); + var size = Bounds.Size; - UIGraphics.BeginImageContextWithOptions (size, false, 0); + UIGraphics.BeginImageContextWithOptions(size, false, 0); - try { - var context = UIGraphics.GetCurrentContext (); + try { + var context = UIGraphics.GetCurrentContext(); - foreach (var window in UIApplication.SharedApplication.Windows) { - if (window.RespondsToSelector (selScreen) && window.Screen != this) - continue; + foreach (var window in UIApplication.SharedApplication.Windows) { + if (window.RespondsToSelector(selScreen) && window.Screen != this) + continue; - context.SaveState (); - context.TranslateCTM (window.Center.X, window.Center.Y); - context.ConcatCTM (window.Transform); - context.TranslateCTM (-window.Bounds.Size.Width * window.Layer.AnchorPoint.X, -window.Bounds.Size.Height * window.Layer.AnchorPoint.Y); + context.SaveState(); + context.TranslateCTM(window.Center.X, window.Center.Y); + context.ConcatCTM(window.Transform); + context.TranslateCTM(-window.Bounds.Size.Width * window.Layer.AnchorPoint.X, -window.Bounds.Size.Height * window.Layer.AnchorPoint.Y); - window.Layer.RenderInContext (context); - context.RestoreState (); - } + window.Layer.RenderInContext(context); + context.RestoreState(); + } - return UIGraphics.GetImageFromCurrentImageContext (); - } finally { - UIGraphics.EndImageContext (); + return UIGraphics.GetImageFromCurrentImageContext(); + } finally { + UIGraphics.EndImageContext (); + } } } } From 66bbd8717f09270fc73e47d959d3e956689b2478 Mon Sep 17 00:00:00 2001 From: Christian Helle Date: Thu, 24 Nov 2016 14:09:28 +0100 Subject: [PATCH 2/2] Fix minor style issue and remove else block --- src/UIKit/UIScreen.cs | 50 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/UIKit/UIScreen.cs b/src/UIKit/UIScreen.cs index fd134310c7bc..d2271da5014a 100644 --- a/src/UIKit/UIScreen.cs +++ b/src/UIKit/UIScreen.cs @@ -37,43 +37,43 @@ public XamCore.CoreAnimation.CADisplayLink CreateDisplayLink (NSAction action) public UIImage Capture () { - if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0)) { + if (UIDevice.CurrentDevice.CheckSystemVersion (7, 0)) { // This is from https://developer.apple.com/library/content/qa/qa1817/_index.html try { var view = UIApplication.SharedApplication.KeyWindow; - UIGraphics.BeginImageContextWithOptions(view.Bounds.Size, view.Opaque, 0); - view.DrawViewHierarchy(view.Bounds, true); - return UIGraphics.GetImageFromCurrentImageContext(); + UIGraphics.BeginImageContextWithOptions (view.Bounds.Size, view.Opaque, 0); + view.DrawViewHierarchy (view.Bounds, true); + return UIGraphics.GetImageFromCurrentImageContext (); } finally { - UIGraphics.EndImageContext(); + UIGraphics.EndImageContext (); } - } else { - // This is from: https://developer.apple.com/library/ios/#qa/qa2010/qa1703.html - var selScreen = new Selector("screen"); - var size = Bounds.Size; + } - UIGraphics.BeginImageContextWithOptions(size, false, 0); + // This is from: https://developer.apple.com/library/ios/#qa/qa2010/qa1703.html + var selScreen = new Selector ("screen"); + var size = Bounds.Size; - try { - var context = UIGraphics.GetCurrentContext(); + UIGraphics.BeginImageContextWithOptions (size, false, 0); - foreach (var window in UIApplication.SharedApplication.Windows) { - if (window.RespondsToSelector(selScreen) && window.Screen != this) - continue; + try { + var context = UIGraphics.GetCurrentContext (); - context.SaveState(); - context.TranslateCTM(window.Center.X, window.Center.Y); - context.ConcatCTM(window.Transform); - context.TranslateCTM(-window.Bounds.Size.Width * window.Layer.AnchorPoint.X, -window.Bounds.Size.Height * window.Layer.AnchorPoint.Y); + foreach (var window in UIApplication.SharedApplication.Windows) { + if (window.RespondsToSelector (selScreen) && window.Screen != this) + continue; - window.Layer.RenderInContext(context); - context.RestoreState(); - } + context.SaveState (); + context.TranslateCTM (window.Center.X, window.Center.Y); + context.ConcatCTM (window.Transform); + context.TranslateCTM (-window.Bounds.Size.Width * window.Layer.AnchorPoint.X, -window.Bounds.Size.Height * window.Layer.AnchorPoint.Y); - return UIGraphics.GetImageFromCurrentImageContext(); - } finally { - UIGraphics.EndImageContext (); + window.Layer.RenderInContext (context); + context.RestoreState (); } + + return UIGraphics.GetImageFromCurrentImageContext (); + } finally { + UIGraphics.EndImageContext (); } } }