17

In my Xcode project I have a blue referenced folder which points to a folder with images.

The benefit of the blue folder is that I don't have to add the files to Xcode manually. So when I add a new image to the folder it automatically shows up in Xcode, however, when I try to reference the file from code using

[UIImage imageNamed:@"myFileFromTheBlueFolder"];

it does not find the image although the top-level folder is added to the target. :-(

Do I have to reference the image differently when I is coming from a blue folder, which would IMHO defeat the purpose of having such a folder at all.

Besi
  • 22,579
  • 24
  • 131
  • 223
  • Add a screenshot of your Xcode file structure showing the image – nduplessis Jul 16 '12 at 07:37
  • 1
    Have you tried a clean? always seem to have to clean lots with blue folders – Martin Jul 16 '12 at 07:39
  • The "blue" folder is a directory. You might be able to (or have to) include that path in the `imageNamed` parameter. Otherwise, you'll have to follow Kevin Ballard's suggestions. You also need the extension of your file. – Rob Jul 16 '12 at 07:46

3 Answers3

30

Kevin is right about the memory optimizations that +[UIImage imageNamed:] provides, but it is absolutely possible to access the images in the blue folders (folder references). All you have to do is to add the folder name into the image name. For example:

Your resources are organized like this:

Icons (blue folder / folder reference)
  |__camera.png
  |__checkmark.png
Shared (yellow folder / xcode group)
  |__back.png
  |__logo.png

You can access the images like this:

[UIImage imageNamed:@"Icons/camera"];
[UIImage imageNamed:@"Icons/checkmark"];

[UIImage imageNamed:@"back"];
[UIImage imageNamed:@"logo"];
Mr. T
  • 12,795
  • 5
  • 39
  • 47
10

I imagine the problem is the "blue folder" (e.g. folder reference, instead of a group) is being copied into your App resources verbatim. In other words, it's not being flattened like resources typically are. If you check your built product, next to all your normal resources you'll probably see the folder itself, and everything that was in the folder is still in the folder.

As such, +[UIImage imageNamed:] can't find the image because it doesn't recurse into subdirectories. You can use -[NSBundle pathForResource:ofType:inDirectory:] to get a path to the resource and pass that to +[UIImage imageWithContentsOfFile:], but this discards the memory optimizations that +imageNamed: has (e.g. where it caches used images and drops unused images).

Alternatively, you could stop using a folder reference. Or you could remove the folder reference from your resources build phase and add a shell script build phase that copies its contents into your built product instead.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • Thanks a lot for your explanation. I guess I will stick with Groups then. I maybe could manage to add the files automatically to the Group using AppleScript or the like. So that I would get the benefits from the blue folders. – Besi Jul 16 '12 at 09:35
  • @Besi: Why are you adding files to your project outside of Xcode? It might be simpler just to teach whomever is adding the files to do so using Xcode directly. – Lily Ballard Jul 16 '12 at 19:34
  • I have to use folder references as some of my image resources have same name. So in my case, how can I achieve/maintain **memory optimizations**? – Vasu Nov 14 '13 at 05:32
  • @Kailash: Use an `NSCache` to hold your images, look them up there first, and then load them with `+[UIImage imageWithContentsOfFile:]` only if they're not already in the cache. – Lily Ballard Nov 14 '13 at 21:24
  • 1
    All you have to do is add the blue folder's (folder reference) name into the path of the image. See my answer for examples. – Mr. T May 30 '14 at 06:35
  • wow old thread. i want to use folder refernces as i have a common lilbrary that i use for apps, if you copy to xcode youhave to manually update all your apps individual, that's a bad thing – μολὼν.λαβέ Dec 25 '16 at 06:59
-10

Write complete name of the image.

[UIImage imageNamed:@"myFileFromTheBlueFolder.png"];

Also keep in mind for case Insensitive names.

Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107