How necessary is it to search for a path to an image using the NSBundle
method pathForResource
when creating a UIImage
using ima
I created a new Xcode project (single view, AppDelelgate, ViewController class, storyboard etc ). Created an Images group. Used Paintbrush to create a 16x16 png file Wall1.png and dropped it into the Images group in Xcode (let Xcode copy the files).
In the ViewController viewDidLoad method added the code:
UIImageView* imageView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 16, 16)];
UIImage *image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Images/Wall1" ofType:@"png"]];
imageView.image = image;
UIImageView* imageView=[[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 16, 16)];
UIImage *image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Wall1" ofType:@"png"]];
imageView.image = image;
[self.view addSubview:imageView];
Ran the app on my phone, the image would not appear
Added a break point at [self.view addSubview:imageView];
image was null
Opened terminal and changed directory to my project, Wall1.png was not a group folder Images. Removed the png from the project, created a Images folder, moved Wall1.png into the folder. Added the existing file Wall1.png to the group Images.
Ran the app, the image still doesn't appear.
image was null
Changed Images/Wall1 to Wall1
Ran the app, boom image is displayed1
If you create a group for your images, Xcode does not create a respective directory. Create one manually if you want to (I prefer to keep my images in a seperate folder). Dont specify the full path to your image file when using UIImage imageWithContentsOfFile.
Try this.
[UIImage imageNamed:@"your directory path of image"]
[UIImage imageNamed:@"Dir1/folder1/folder2/imagename.jpeg"]
The docs say that “the method looks for an image with the specified name in the application’s main bundle”, so I’d say you can always use just the name. The only exception might be images stored inside subfolders, especially when you have foo/image.png
and bar/image.png
. I don’t know whether [UIImage imageNamed:@"foo/image"]
would work, but it’s trivial to try.
(What’s a bit confusing in these cases is that the groups in the Xcode tree do not correspond to folders in the resulting app bundle. Their contents are smashed together to the root of the bundle, unless you use a blue folder reference instead of a regular group.)
Not at all ... is the answer to the original question:
How necessary is it to search for a path to an image using the NSBundle method pathForResource when creating a UIImage using imageNamed?
Not much .. is how correct the accepted answer from Zoul and the other one from Ranga are. To be fair: they are correct if you're talking about the application bundle directory structure, or for the (rare) case where the image is in a "blue" folder in Xcode (more on that later), but not for the most common cases
Anyway, on to the one true answer.
As usual I found this question while trying to find the answer myself. I never found the documentation or the other answers to this question satisfactory, so I decided to test.
My test details are all below, but let me summarize the results here. In short, when using imageNamed: to load your images, it depends where you put them:
if your images are in the root of your project, even if organized in a purely logical Xcode group, then no, you don't need to think about the path: just the image name.
if your images are in a group that is attached to a directory in your file system, via "create groups for added folders" then you still don't need to worry about the name.
if your images are in a "blue" group, that is attached to a directory in your file system via "create folder references for added folders", then you can load it with imageNamed: by specifying a relative path as suggested (coincidentally?) by the accepted answer above.
if you use the main alternative to imageNamed:, imageWithContentsOfFile:, you do indeed need the full path to the file including the bundle path, which means you need to know how the Xcode navigator structure translates into paths in your bundle directory structure.
The other important differences between these two methods:
So on with my tests.
What I did was create a simple UITableView app with 3 simple icon files, shown in the rows of the table using different methods. The icons are different in their location in the Xcode project structure. Note the emphasis on Xcode. The key to understanding the answer to the original question is that there are three totally different project directory structures in an iOS app: There's the one you see in the Xcode navigator, the one on the file system for the same project that you see in Finder (right click any item in the Xcode navigator and select "show in Finder") and, the one you seldom see, the "bundle" directory structure of the deployed app. You can see this last one in Finder too - by finding your app in ~/Library/Application Support/iPhone Simulator, and drilling down into the .app directory. I'll show you a picture of mine in a minute.
So in my app, I dragged all three icon png image files into Xcode in different ways:
icon1.png (a clock), I dragged in as a file to the root of the Xcode project, then I later created a new group in Xcode and dragged it into that. This group is not represented by any directory in the file system: it's a pure Xcode group. Hence it's name: "JustGroup"
icon2.png (an eye), I originally put my file system in directory called "RealDir", and I dragged this entire directory into Xcode, and when asked, I chose the "Create groups for any added folders" option. This means that the RealDir group in Xcode is attached to a real directory called RealDir in the filesystem (in my project directory) and that icon2.png is in there.
icon3.png (a target), I also had in a separate directory, which I also dragged into Xcode. Only this time I chose the 2nd radio option "Create folder references for any added folders". This creates a so-called "blue" group in Xcode. More on what this is all about later. I called this group (and directory) "FolderReference"
Here's a shot of the choice that Xcode gives you:
And here's what my project structure looks like in Xcode:
Now, in my app, I used two methods for loading each of the icons: UIImage imageNamed: and UIImage imageWithContentsOfFile. I created a bunch of rows in my table with the title of the each cell being the name of the group containing the icon: JustGroup, RealDir or FolderReference, plus the name of the method used: imageNamed vs fromFile (which I'm using as abbreviation of imageWithContentsOfFile)
The detail label of the cell (the fainter text under the title) shows the file or path name I gave to the method.
To be clear, in the case of "fromFile", I'm adding the bundle path to the "relative" name you see. So for "fromFile", I'm actually using this code:
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *imagePath = [NSString stringWithFormat:@"%@/%@", bundlePath, filePath];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
where "filePath" is the path you see in the table cell detail label. For imageNamed:, on the other hand, the filePath in the cell detail is passed verbatim.
And the row image is, naturally, the image that is loaded. So for the rows in the table that have no image, the image loading failed.
Here, in a nutshell, are the results. If you read nothing of this post, at least a glance at this image will tell you all you need to know.
Here's the basic explanation in easily digestible points:
as it states in the official documentation, the imageNamed: method loads images from the application bundle. This means you don't need to specify the bundle location, only the file name. And even then, just the base name of the file. The documentation is a little thin here, it should really make it clear that it loads the image from the given file path relative to the application bundle root directory.
(here's the kicker, pay attention to this one) that rule about the bundle directory, refers the root directory in your the bundle of your deployed app. If you go exploring, that means in the ".app" directory itself. That's not the same as the root directory of the Xcode project in the Xcode navigator, nor is it the same as the root directory of the Xcode project in finder
this is because, when deploying your app to the device (or simulator) all project directories represented by "groups for added folders" are flattened. That is, the directory is ignored, and all of its contents dumped unceremoniously into the root directory of the bundle. (I say "unceremoniously" because if there are files with the same name in different folders, they'll collide here and you'll get no help in resolving the problems that causes.) This is the case of RealDir in my example: in the deployed app, RealDir no longer exists, and icon2.png is left to mix in with the general population (scary). It goes almost without saying that "JustGroup", the purely logical Xcode group, is also ignored - it was never a real directory anyway, just a visual aid to the Xcode user - and icon1.png is also in the bundle root.
This is why imageNamed: was able to load icon2.
And also why imageWithContentsOfFile was not able to find it in "RealDir/image2.png": because there is no RealDir directory in the deployed app.
"blue folders", on the other hand, that is, directories represented by "folder references for added folders", are in fact retained in the app bundle directory structure. This, apparently is the point of blue folders: they give you a way to create a directory structure in your deployed app. I'm not sure of the original raison d'etre for this, but one good use case is where you have several directories containing alternative versions of resource files with the same name and you want your app to be able to switch between them at runtime by changing directory. Anyway, the icon3.png in my FolderReference, remained in my FolderReference directory in the deployed app.
To clarify, here are my folder structures:
You saw my Xcode project navigator structure above, here is the file system directory underneath it:
And finally, perhaps most importantly, the deployed bundle file system directory structure:
Note: I found this at this location on my Mac: you will find yours in a similar location - you might have to search a bit to find which ugly-GUID-named subdirectory contains your app.
~/Library/Application Support/iPhone Simulator/5.1/Applications/4EB386B2-CD7E-4590-9757-18DDDEE6AF4F/ImageLoadingTest.app
I hope this helps. Testing, exploring and finally, describing it certainly helped me.