Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 7 additions & 38 deletions src/UIKit/UIScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,13 @@ public CoreAnimation.CADisplayLink CreateDisplayLink (Action action)
/// </remarks>
public UIImage Capture ()
{
if (SystemVersion.CheckiOS (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 ();
}
}

// 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);

try {
var context = UIGraphics.GetCurrentContext ();

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

window.Layer.RenderInContext (context);
context.RestoreState ();
}

return UIGraphics.GetImageFromCurrentImageContext ();
} finally {
UIGraphics.EndImageContext ();
}
// This is from https://developer.apple.com/library/content/qa/qa1817/_index.html
// Updated to use UIGraphicsImageRenderer to avoid deprecated UIGraphicsBeginImageContextWithOptions
var view = UIApplication.SharedApplication.KeyWindow;
using var renderer = new UIGraphicsImageRenderer (view.Bounds.Size);
return renderer.CreateImage ((context) => {
view.DrawViewHierarchy (view.Bounds, true);
});
}
}
}
11 changes: 3 additions & 8 deletions src/UIKit/UIView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,11 @@ public static Task<bool> AnimateAsync (double duration, Action animation)
/// </remarks>
public UIImage Capture (bool afterScreenUpdates = true)
{
UIImage snapshot;
var bounds = Bounds; // try to access objc the smalles amount of times.
try {
UIGraphics.BeginImageContextWithOptions (bounds.Size, Opaque, 0.0f);
using var renderer = new UIGraphicsImageRenderer (bounds.Size);
return renderer.CreateImage ((context) => {
DrawViewHierarchy (bounds, afterScreenUpdates);
snapshot = UIGraphics.GetImageFromCurrentImageContext ();
} finally {
UIGraphics.EndImageContext ();
}
return snapshot;
});
}

#region Inlined from the UITraitChangeObservable protocol
Expand Down
Loading