Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage imageNamed: does not find image within blue referenced Xcode-folder

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.

like image 896
Besi Avatar asked Jul 16 '12 07:07

Besi


2 Answers

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"];
like image 65
Mr. T Avatar answered Nov 15 '22 18:11

Mr. T


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.

like image 24
Lily Ballard Avatar answered Nov 15 '22 17:11

Lily Ballard