UIImage imagenamed: load wrong UIImage

I had a main project contain more than 10 bundles. I had two image named "map_bubble_right_normal", one in main bundle, the other one in sub bundle(SCommon.bundle). I write code as follow:


UIImage *image = [UIImage imageNamed:@"map_bubble_right_normal"];


The code was writen in didFinishLaunchingWithOptions of AppDelegate.


I want to load the image which I saved at main bundle. However, the result of the code that load image from sub bundle(SCommon.bundle).


I guess that image saved at main bundle maybe a error image(maybe copy problem). Therefore, I show the ipa content to see the "map_bubble_right_normal" file saved at root directory. The image is right!!!!!


I don't know why `imageNamed:` would load the image from sub bundle(SCommon.bundle). I tried to delete app and restart iPhone, the result is same. And, I tried to delete Derived Data of XCode, and clean the project. The image still load from sub bundles(SCommon.bundle).


Addition, I test the problem at iOS 8 and iOS 9 device.


Only a method which change the bundleid(main bundle) can solve the problem temporary so far.


I know the cache feature of UIImage, but I can't understand why did the strange scene will happen.


I hope Apple Developer & Engineer will help me to solve the problem. (Emergency)


Thank you~~~

Replies

Try explicitly specifying the bundle to use, like this:

UIImage *image = [UIImage imageNamed:@"map_bubble_right_normal" inBundle:NSBundle.mainBundle compatibleWithTraitCollection: nil];

I didn't want to replace `imageNamed:` method, I just want to found why `imageNamed` would load error image. Thank you for your answer!

I tried your method, failed either.

+[NSImage imageNamed:] tries to find the right bundle to load the image from. Usually it's an easy choice, but in your case I guess it's having a bit of trouble. I ran into an issue like this with an asset catalog embedded in a framework (see this thread for more about that), and I suspect something similar is happening to you.


Before you go further, I recommend that you double-check your assets to make sure that you haven't accidentally put the wrong one in your app's main bundle. It could be as simple as that.

Thank you for your answer! I found the cause of the problem accorrding to your tip. The Error Image isn't came from sub bundle, which came from Assets.car. The cause of the problem should be blame to Image.assert in physical directory actually. CocoaPods will copy all match "*.assets" to Assets.car.

Glad you were able to figure it out! 🙂

And yeah, I don't blame you for being a bit confused about how all this works—as you've discovered, it's quite complicated.