[UIKit] Fix crash in UIScreen.Capture() and UIView.Capture() by replacing deprecated UIGraphicsBeginImageContextWithOptions with UIGraphicsImageRenderer. Fixes #24053.#24054
Conversation
…hicsImageRenderer in UIScreen and UIView Capture methods Co-authored-by: rolfbjarne <249268+rolfbjarne@users.noreply.github.com>
✅ [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 |
|
I tested this locally, and the screenshot for a test app looked identical before and after this change. |
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?