Commit 2a7416f
## Problem
Customers were experiencing crashes when taking screenshots in iOS apps with the following error:
```
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.
```
The crash was occurring in both `UIScreen.Capture()` and `UIView.Capture()` methods, which were using the deprecated `UIGraphicsBeginImageContextWithOptions` API. Apple explicitly recommends using `UIGraphicsImageRenderer` instead to avoid these allocation failures.
## Solution
Replaced the deprecated `UIGraphicsBeginImageContextWithOptions` API with the modern `UIGraphicsImageRenderer` API in both affected methods:
### UIScreen.Capture()
- Removed legacy iOS version checks and fallback code paths
- Simplified from ~40 lines to a clean 9-line implementation
- Now uses `UIGraphicsImageRenderer` to capture the KeyWindow
### UIView.Capture(bool afterScreenUpdates)
- Replaced try-finally block with using statement for `UIGraphicsImageRenderer`
- Maintained the `afterScreenUpdates` parameter functionality
- Cleaner, more maintainable code
## Benefits
- ✅ Eliminates the CGBitmapContext allocation crash reported by users
- ✅ Uses Apple's recommended modern API (available on all supported iOS versions)
- ✅ Simplifies code by removing legacy version checks
- ✅ More robust and efficient image rendering
- ✅ Follows Apple's guidelines from the error message itself
## References
- Issue: #24053
- Apple Documentation: [UIGraphicsImageRenderer](https://developer.apple.com/documentation/uikit/uigraphicsimagerenderer)
- Suggested by @rolfbjarne
Fixes #24053.
---------
Co-authored-by: rolfbjarne <[email protected]>
1 parent 056502e commit 2a7416f
2 files changed
+10
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
82 | 51 | | |
83 | 52 | | |
84 | 53 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
255 | | - | |
256 | 255 | | |
257 | | - | |
258 | | - | |
| 256 | + | |
| 257 | + | |
259 | 258 | | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
| 259 | + | |
265 | 260 | | |
266 | 261 | | |
267 | 262 | | |
| |||
0 commit comments