Crash When Run app On Xcode15 Simulator

When UICollectionViewFlowLayoutAlertForInvalidItemSize Occured Some Log Like These Occurred, and then app crash

*** Assertion failure in void _UIGraphicsBeginImageContextWithOptions(CGSize, BOOL, CGFloat, BOOL)(), UIGraphics.m:410

crash Output

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={0, 16}, scale=3.000000, bitmapInfo=0x2002. Use UIGraphicsImageRenderer to avoid this assert.'

I've run into this error as well. Has anyone figured out a workaround? Should I just download xcode 14? Or is the issue with Ios 17? I currently am using React Native, so I don't think I have control over the UIGraphicsBeginImageContext() stuff.

I've checked my ui elements and I don't think anything is set to Width: 0 or Height: 0.

I had the same issue in my react-native project when running the app on iOS 17 simulator. After banging my head for 2 days I finally fixed this issue.

Reason The reason for this error is that the UIGraphicsBeginImageContext and UIGraphicsBeginImageContextWithOption are depreciated.

Fix To fix this issue either remove, comment or replace UIGraphicsBeginImageContext and UIGraphicsBeginImageContextWithOption with UIGraphicsImageRenderer. To do this here are the steps that I followed.

  1. First search the whole project with the command grep -r "UIGraphicsBeginImageContext" ./ this will list all the files using UIGraphicsBeginImageContext or UIGraphicsBeginImageContextWithOption. This step is important as you won't know how many packages are there which refrences the UIGraphicsBeginImageContext. In my case I had several react-native packages as well as pods having UIGraphicsBeginImageContext

  2. Then one-by-one remove or replace it with UIGraphicsImageRenderer. Replacing with UIGraphicsImageRenderer will be tricky if you don't have good idea of objective c. So, I suuggest first commenting and then checking if hte solution works for you.

  3. Clean project, delete the app from simulator and then reinstall. Hopefully this will fix the issue. It did for me.

For expediency (and backward compatibility), you could find all occurrences of UIGraphicsBeginImageContext in your code, and add a size check just beforehand, returning early with a nil image if the size is invalid. For example:

    if (size.width <= 0 || size.height <= 0) { return nil; }
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);

It occurs when running a simulator with iOS 17. Down stepping the simulator to pre iOS 17 isn't much use if you want to release to production.

For me, using React Native, patching source is not a nice option. However, as suggested elsewhere, I was able to search for UIGraphicsBeginImageContext in the code and discover that several libraries were needing updated, react-native-svg, react-native-linear-gradient, react-native-vector-icons and I had to replace the deprecated react-native-camera.

On react native it turns out to be FastImage package issue, refer to https://github.com/DylanVann/react-native-fast-image/issues/1002

From my side I was facing due to incompatibility of Pod (Dynatrace). I just used pod from dynatrace document and issue got fixed for me. Previously pod was used from any other website.

use UIGraphicsImageRenderer instead of UIGraphicsBeginImageContext, I faced same crash and i fixed that.

We are also seeing crashes with UIGraphicsBeginImageContext on iOS 17 devices. At first we also thought that it would happen because of a 0 in the width or height of the size. But it crashes in one app at this point:

    if(size.width < 50 || size.height < 50)
    {
        return nil;
    }
    
    size = CGSizeMake(size.width*2.0f, size.height*2.0f);
    
    UIGraphicsBeginImageContext(size);

With this code, it is impossible for the width or height to be 0. The crash must therefore also have other causes. Unfortunately, we cannot reproduce the crash with our test devices.

Incident Identifier: AAAAAAAAAA-893D-4C7B-A933-AAAAAAAAAA
Hardware Model:      iPad13,16
Process:             AAAAAAAAAA [37228]
Path:                /private/var/containers/Bundle/Application/AAAAAAAAAA-F8FB-405B-8DC7-AAAAAAAAAA/AAAAAAAAAA.app/AAAAAAAAAA
Identifier:          AAAAAAAAAA.AAAAAAAAAA.AAAAAAAAAA
Version:             13.6 (1.0)
AppStoreTools:       15A240a
AppVariant:          1:iPad13,16:15
Code Type:           ARM-64 (Native)
Role:                Background
Parent Process:      launchd [1]
Coalition:           AAAAAAAAAA.AAAAAAAAAA.AAAAAAAAAA [700]

Date/Time:           2023-11-24 21:03:57.5000 -0500
Launch Time:         2023-11-24 08:47:34.0066 -0500
OS Version:          iPhone OS 17.1.1 (21B91)
Release Type:        User
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: SIGNAL 6 Abort trap: 6
Terminating Process: AAAAAAAAAA [37228]

Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                	0x18863c870 __exceptionPreprocess + 164 (NSException.m:249)
1   libobjc.A.dylib               	0x1809a7c00 objc_exception_throw + 60 (objc-exception.mm:356)
2   Foundation                    	0x187ba6e54 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 172 (NSException.m:261)
3   UIKitCore                     	0x18a8683a4 _UIGraphicsBeginImageContextWithOptions + 568 (UIGraphics.m:407)
4   AAAAAAAAAA                    	0x100a9e83c -[AAAAAAAAAA getAaaaaImage:] + 88 (AAAAAAAAAA.m:7815)
5   AAAAAAAAAA                    	0x100a9e6b0 -[AAAAAAAAAA createAaaaa] + 92 (AAAAAAAAAA.m:7783)
6   AAAAAAAAAA                    	0x100a8b14c -[AAAAAAAAAA setAaaaaSize:] + 2664 (AAAAAAAAAA.m:2566)
7   AAAAAAAAAA                    	0x100a84164 -[AAAAAAAAAA viewDidLayoutSubviews] + 284 (AAAAAAAAAA.m:890)
8   UIKitCore                     	0x18a7a67f0 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1876 (UIView.m:20001)
9   QuartzCore                    	0x189bbc1c0 CA::Layer::layout_if_needed(CA::Transaction*) + 500 (CALayer.mm:10804)
10  UIKitCore                     	0x18a7a9744 -[UIView(Hierarchy) layoutBelowIfNeeded] + 296 (UIView.m:14119)
11  UIKitCore                     	0x18a7ac3e8 -[UINavigationController _layoutViewController:] + 816 (UINavigationController.m:7728)
12  UIKitCore                     	0x18a7ac0b4 -[UINavigationController _layoutTopViewControllerLookForNested:] + 436 (UINavigationController.m:5626)
13  UIKitCore                     	0x18a9392a0 __105-[UINavigationController _repositionPaletteWithNavigationBarHidden:duration:shouldUpdateNavigationItems:]_block_invoke + 440 (UINavigationController.m:3149)

If you use React-Native and dependency react-native-fast-image, follow this issue. i solved the same problem with this help.

https://github.com/DylanVann/react-native-fast-image/issues/1002

Do you still observe this problem? In crashlytics, I only see this problem with iOS 17.0.x versions. I think since iOS 17.1 this problem is fixed.

I had the same issue in my project when running the app on iOS 17 Device/Simulator. Reason is "UIGraphicsBeginImageContext" is depreciated with Xcode 15.0 and Later versions.

So, I was find the all "UIGraphicsBeginImageContext" in my App and framework and COMMENTED. After that, Clean all builds (App and FW) and regenerate both. Finally, reinstall. Hopefully this will fix the issue. It's works for me.

We migrated all UIGraphicsBeginImageContext code in all our apps to UIGraphicsImageRenderer and the crashes disappeared. Now everything is good again.

    UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat];
    format.opaque = NO;
    format.scale = 1;
    format.preferredRange = UIGraphicsImageRendererFormatRangeStandard; 

    UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:imageSize format:format];

    UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) 
    {
        CGContextRef context = rendererContext.CGContext;

        
    }];

We are aslo facing same issue on xcode 15

Update react-native-linear-gradient to version 2.8.3.

this problem may also be related to the react-native version. I am using react native version 0.68.1 and in this version UIGraphicsBeginImageContext() methods are used. new react native versions do not use these methods.

If your project is small, you can switch to the new react native version.

You can check this video as a solution: https://www.youtube.com/watch?v=psIBtbwMLQE

Crash When Run app On Xcode15 Simulator
 
 
Q