Related APIs:
+[UIImage imageNamed:]
+[UIImage imageNamed:inBundle:compatibleWithTraitCollection:]
When there is only a color set with the name (no image set provided), reading the image with the above-mentioned APIs throws an exception:
2023-12-19 19:30:34.008701+0800 ***[57410:578064] *** Assertion failure in -[_UIImageCGImageContent initWithCGImageSource:CGImage:scale:], _UIImageContent.m:666
2023-12-19 19:30:34.031207+0800 ***[57410:578064] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Need an imageRef'
Even when wrapped in a try/catch block, it still crashes due to a lock issue after calling the related API twice. Here is an example code snippet:
UIImage *image;
for (NSInteger i=0; i<10; i++) {
@try {
image = [UIImage imageNamed:@"sample"];
//image = [UIImage imageNamed:@"sample" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
} @catch (NSException *exception) {
}
}
The crash backtrace is as follows:
#0 0x0000000102b12214 in _os_unfair_lock_recursive_abort ()
#1 0x0000000102b0da50 in _os_unfair_lock_lock_slow ()
#2 0x000000010ab07624 in -[UIImageAsset imageWithConfiguration:] ()
#3 0x000000010b2f4634 in -[_UIAssetManager imageNamed:configuration:] ()
#4 0x000000010aaf2394 in +[UIImage imageNamed:inBundle:withConfiguration:] ()
#5 0x000000010aaf2200 in +[UIImage imageNamed:inBundle:compatibleWithTraitCollection:] ()
#6 0x0000000105442154 in +[UIImageAccessibility imageNamed:inBundle:compatibleWithTraitCollection:] ()
It seems that the color set is being treated as an imageRef in this image reading process.
It's a bug of UIKit?