1

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 sincerely request you a great god for help. Thank you~

Startry
  • 574
  • 1
  • 4
  • 17
  • What do you mean by "more than 10 bundles"? What are these other (non app) bundles and how do they relate to the app? – trojanfoe Mar 15 '16 at 10:12
  • Seems that your "sub bundle" ist actually your main bundle. The documentation clearly states that imageNamed: looks in the main bundle. It should act the same for other images. – ff10 Mar 15 '16 at 10:18
  • Sorry, I just want to know why this happen. I know how to avoid the problem. – Startry Mar 15 '16 at 10:31
  • @ff10 I sure "sub bundle" isn't main bundle. Actually, `imageNamed:` load image from `sub bundle` at some case. – Startry Mar 15 '16 at 10:35
  • @trojanfoe "more than 10 bundle" make no sense in here, only one sub bundle contains same image name. The sub bundle copy and maintain by CocoaPods Repo. I add bundle to main project by podspec desc. eg: `s.resource_bundles = { 'SCommon' => ['SCommon/Pod/Assets/**] }` – Startry Mar 15 '16 at 10:37

4 Answers4

1

Thank you for everyone's answer. I sorry about I had mislead all of us to bundle.

I found the cause of the problem. 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.

I had made same question at Apple Developer forum, and found the key tip of the problem.

The issue of Cocoapods which relative to the problem Pods copy resource script overrides default xcasset bahaviour.

Pod_resources.sh script would copy every *.xcassets in Pod_Root if your Pod Repo had add any folder which name match *.xcassets.

Key Code of Pod_resources.sh:

if [[ -n "${WRAPPER_EXTENSION}" ]] && [ "`xcrun --find actool`" ] && [ -n "$XCASSET_FILES" ]
   ...
   # Find all other xcassets (this unfortunately includes those of path pods and other targets).
   OTHER_XCASSETS=$(find "$PWD" -iname "*.xcassets" -type d)
   while read line; do
   if [[ $line != "`realpath $PODS_ROOT`*" ]]; then
      XCASSET_FILES+=("$line")
   fi
   ... 
fi

Condition XCASSET_FILES will be fulfill if your Pod had add any xcassets file.

Startry
  • 574
  • 1
  • 4
  • 17
0

To load image from main bundle try this:

UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"map_bubble_right_normal" ofType:@"png"]];
gvuksic
  • 2,983
  • 3
  • 32
  • 37
  • Sorry, I want to know why this will happen. I don't want to replace `imageNamed:` – Startry Mar 15 '16 at 10:33
  • @Startry according to docs: "If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle." - is image from other bundle loaded earlier? is there something you do with other bundle images before this? – gvuksic Mar 15 '16 at 10:41
  • I tried delete all derived data of XCode, clean the project, delete the app of device, then build the app to my device. It can't be cached if `imageNamed` work property like docs. – Startry Mar 15 '16 at 10:43
0

If you want to load from mainBundle try this code:

NSBundle* myBundle = [NSBundle mainBundle];
NSString* myImage = [myBundle pathForResource:@"Seagull" ofType:@"jpg"];

If you want to load from sub bundle:

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"YourBundleName" ofType:@"bundle"];
NSBundle *subBundle = [NSBundle bundleWithPath:bundlePath];
NSString* myImage = [subBundle pathForResource:@"Seagull" ofType:@"jpg"];

Refer here

Nhat Dinh
  • 3,378
  • 4
  • 35
  • 51
  • Sorry, I just want to know why this happen. I known how to avoid the problem. – Startry Mar 15 '16 at 10:32
  • So what do you want to know is why `imageNamed ` load image from sub bundle but not from mainBundle ? – Nhat Dinh Mar 15 '16 at 10:36
  • Yes. if the code work property, it should load image from main bundle. But, the image load from other bundle. – Startry Mar 15 '16 at 10:39
  • Its better if you make the problem clear. Check bellow link, i think you will get what you want. https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImage_Class/#//apple_ref/occ/clm/UIImage/imageNamed: – Nhat Dinh Mar 15 '16 at 10:48
  • I had visited the doc of UIKit many times, but I couldn't found what I want. I had ask the same problem at https://forums.developer.apple.com/message/124162#124162 too. I can't make a simple demo because the problem occur by accidental. I assume that the `imageNamed` would be effected by cache of last version app installed in device. Anyway, thank you for your reply~ – Startry Mar 15 '16 at 10:55
0

I've occured some same situation. Instead of two bundle you mentioned above, I failed to show the right image with the exact right name. And finally, I found the key point is that the image has a suffix of jpg instead of png. I changed to an image of png and all turned well. Maybe it's a possible situation you occured. Have a try and good luck. :)

PS: Here's a related link as a reference.

Community
  • 1
  • 1
Calios
  • 806
  • 9
  • 23