Skip to content

Crash: UIKit - UIScreen - Capture: UIGraphicsBeginImageContext() failed to allocate CGBitampContext #24053

@bnSonic

Description

@bnSonic

Apple platform

iOS

Framework version

net9.0-*

Affected platform version

VSCode on mac; .net9 maui 9.0.110

Description

every few days i get a crash from customers of my App that reads like this:

Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={1080, 810}, scale=2.000000, bitmapInfo=0x2006. Use UIGraphicsImageRenderer to avoid this assert.
Native stack trace:
	0   CoreFoundation                      0x000000018ef55de8 2744BAE5-E258-3D88-B1E7-17A118109A6F + 1113576
	1   libobjc.A.dylib                     0x000000018c6e1d8c objc_exception_throw + 72
	2   Foundation                          0x000000018e2b36c8 519FF073-0591-3EF4-8FE1-4AA7226AC834 + 7268040
	3   UIKitCore                           0x00000001917e8704 A6B14CA2-D4B7-3636-96A8-2280A3954EF4 + 2746116
	4   mycompany.myapp.ios                   0x0000000103582198 wrapper_managed_to_native_UIKit_UIGraphics_BeginImageContextWithOptions_CoreGraphics_CGSize_byte_System_Runtime_InteropServices_NFloat + 168
	5   mycompany.myapp.ios                   0x0000000106e61ac8 Microsoft_iOS_UIKit_UIScreen_Capture + 1196
	6   mycompany.myapp.ios                   0x0000000102e3c328 DATAflor_Common_iOS_DependencyService_DFiOSScreenshotService_TakeScreenshot_object + 88
	7   mycompany.myapp.ios                   0x000000010419d298 DATAflor_Time_iOS_DATAflor_Time_iOS_CustomUIApplication__c__DisplayClass8_0__OnDisplayTouchb__0 + 88
	8   mycompany.myapp.ios                   0x00000001071e3ae8 -[CustomUIApplication sendEvent:] + 48

we are doing screenshots in our app for Hotline-Service-purposes.
now with maui is see these crashes (didn't find such crashes with the old xamarin app but i think it was build with an older XCode too)

When i look at the code here on github (UIKit/UIScreen - Capture) is see this:

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

As far as i'm aware, BeginImageContextWithOptions is obsolete / deprecated?! wasn't that documented somewhere at apple and discussed in some maui-issue-threads? :)

so i wonder if this could be changed here and if this would fix that crash?
Or if someone could tell me why it ISN'T changed here?

Thanks a lot

Steps to Reproduce

don't know - write an app that calls ".Capture()" in the iOS-Part of a maui app running on iOS (iPad, iPhone)? It doesn't happen every time but i see it every few days when my customers are reporting it.

Did you find any workaround?

not yet

Relevant logs

see issue with the log inside

Metadata

Metadata

Assignees

Labels

enhancementThe issue or pull request is an enhancement

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions