Startry
Startry

Reputation: 584

UIImage load wrong image in main bundle

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~

Upvotes: 1

Views: 1527

Answers (4)

Startry
Startry

Reputation: 584

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.

Upvotes: 1

Calios
Calios

Reputation: 846

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.

Upvotes: 0

Nhat Dinh
Nhat Dinh

Reputation: 3447

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

Upvotes: 0

gvuksic
gvuksic

Reputation: 3013

To load image from main bundle try this:

UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"map_bubble_right_normal" ofType:@"png"]];

Upvotes: 0

Related Questions