-
Couldn't load subscription status.
- Fork 549
Fix crash in UIScreen.Capture() and UIView.Capture() by replacing deprecated UIGraphicsBeginImageContextWithOptions with UIGraphicsImageRenderer. Fixes #24053. #24054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…hicsImageRenderer in UIScreen and UIView Capture methods Co-authored-by: rolfbjarne <[email protected]>
✅ [CI Build #1b062e4] Build passed (Build packages) ✅Pipeline on Agent |
✅ [PR Build #1b062e4] Build passed (Detect API changes) ✅Pipeline on Agent |
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
✅ [CI Build #1b062e4] Build passed (Build macOS tests) ✅Pipeline on Agent |
💻 [CI Build #1b062e4] Tests on macOS X64 - Mac Sonoma (14) passed 💻✅ All tests on macOS X64 - Mac Sonoma (14) passed. Pipeline on Agent |
💻 [CI Build #1b062e4] Tests on macOS M1 - Mac Monterey (12) passed 💻✅ All tests on macOS M1 - Mac Monterey (12) passed. Pipeline on Agent |
💻 [CI Build #1b062e4] Tests on macOS arm64 - Mac Sequoia (15) passed 💻✅ All tests on macOS arm64 - Mac Sequoia (15) passed. Pipeline on Agent |
💻 [CI Build #1b062e4] Tests on macOS M1 - Mac Ventura (13) passed 💻✅ All tests on macOS M1 - Mac Ventura (13) passed. Pipeline on Agent |
💻 [CI Build #1b062e4] Tests on macOS arm64 - Mac Tahoe (26) passed 💻✅ All tests on macOS arm64 - Mac Tahoe (26) passed. Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
🚀 [CI Build #1b062e4] Test results 🚀Test results✅ All tests passed on VSTS: test results. 🎉 All 115 tests passed 🎉 Tests counts✅ cecil: All 1 tests passed. Html Report (VSDrops) Download Pipeline on Agent |
Problem
Customers were experiencing crashes when taking screenshots in iOS apps with the following error:
The crash was occurring in both
UIScreen.Capture()andUIView.Capture()methods, which were using the deprecatedUIGraphicsBeginImageContextWithOptionsAPI. Apple explicitly recommends usingUIGraphicsImageRendererinstead to avoid these allocation failures.Solution
Replaced the deprecated
UIGraphicsBeginImageContextWithOptionsAPI with the modernUIGraphicsImageRendererAPI in both affected methods:UIScreen.Capture()
UIGraphicsImageRendererto capture the KeyWindowUIView.Capture(bool afterScreenUpdates)
UIGraphicsImageRendererafterScreenUpdatesparameter functionalityBenefits
References
Fixes #24053.
Original prompt
This section details on the original issue you should resolve
<issue_title>Crash: UIKit - UIScreen - Capture: UIGraphicsBeginImageContext() failed to allocate CGBitampContext</issue_title>
<issue_description>### 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:
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:
As far as i'm aware,
BeginImageContextWithOptionsis 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</issue_description>
<agent_instructions>The
Capturemethod should be updated, and there are two versions:macios/src/UIKit/UIScreen.cs
Lines 42 to 83 in c7579d1
macios/src/UIKit/UIView.cs
Lines 253 to 265 in c7579d1
Update both to use my suggestion from my first comment in this issue.</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@rolfbjarne This is completely untested, but can you try this and see if it works for you?