25

I have a bundle where i put images in it.

The contents are:

MyBundle.bundle/images/picture1.png
MyBundle.bundle/images/picture2.png

I dragged MyBundle.bundle into my project.

Now I can see these images in Interface Builder and can even use them. However, when I run the app, I don't see the images.

What's wrong?

Thanks,

Van Du Tran
  • 6,736
  • 11
  • 45
  • 55

3 Answers3

39

Make sure the bundle is getting copied over in the "Build Phases" of the project settings for the target.

Also, try setting them progrmatically and see if they show:

myImageView.image = [UIImage imageNamed:@"MyBundle.bundle/images/picture1.png"];

I just tried this in my project and found that what you need to do is specify the image with the bundle name in front like this:

Image in IB

In the IB the image will look broken:

Broken

but when you build and run your project the image will show correctly.

So, in your case, use MyBundle.bundle/images/picture1.png in the actual Interface Builder Image box.

Also, when you dont put the bundle name in front, you get this warning when you load the view with the bundle image:

2011-10-12 08:22:16.360 UTDebug[721:11c03] Could not load the "map.png" image referenced from a nib in the bundle with identifier "com.companyname.myproject"
chown
  • 51,908
  • 16
  • 134
  • 170
  • Hello. Yes the line of code myImageView.image = [UIImage imageNamed:@"MyBundle.bundle/images/picture1.png"]; works. However, if I set the image in IB, I get this warning in the log: Could not load the "picture1.png" image referenced from a nib in the bundle with identifier "com.xxx" and it does not show up. I did check the "Build Phases" and the bundle is in there. Any other suggestions? Did you manage to make it work yourself? – Van Du Tran Oct 12 '11 at 14:45
  • Ok, I found the issue. You need to put the bundle name in the actual image box in the IB. See my answer edits for more. – chown Oct 12 '11 at 15:26
  • Thanks! It worked! They should fix the issue of the image looking _broken_ in IB as it's misleading! Thanks again! – Van Du Tran Oct 12 '11 at 20:21
  • Yea, they should. Might look into filing a bug report with apple if there isn't one already. Glad you got it fixed though! – chown Oct 12 '11 at 20:23
  • ... but half of the point of setting up images in IB is defeated if you can't see the image ... – Glenn Maynard Aug 24 '12 at 02:36
  • @Glenn Agreed, Apple doesn't seem to put much effort into developing Xcode. – chown Aug 24 '12 at 06:06
  • @chown, What if I dont want to write the whole path of the image but just the name? Because there are so many images that it will take a lot of unnecessary job. – Carusd Ray Apr 12 '13 at 10:07
  • @CarusdRay Ask the Xcode Devs to fix their broken shit? Seriously though, what I would do is write a quick python script to automate the inserting of image names into the project XML/plist. – chown Apr 12 '13 at 14:39
  • Any idea how to access the image if it is inside an asset catalog within another bundle? – jowie Dec 02 '15 at 17:06
  • 3
    Even if I add `FrameworkName.bundle/imageName.png` in IB, this doesn't work for me unfortunately. I also tried with the identifier (com.name etc) but that didn't work either... – jowie Dec 03 '15 at 16:11
  • 3
    This doesn't work when the images are in the other bundle's xcassets. Is there a way to specify that? – Graham Perks May 10 '18 at 14:13
9

You need to put the XIB in the bundle, too. When you do that, resources for the interface will be relative to the bundle, so they'll work both in IB and when you load the interface in your app.

Glenn Maynard
  • 55,829
  • 10
  • 121
  • 131
0

You might have a stray build setting from having created the bundle as a Mac OS X target.

See my other answer for the solution.

Community
  • 1
  • 1
Frank Schmitt
  • 25,648
  • 10
  • 58
  • 70