In Part 1 of Xcode, Folders and the File System I walked through a short example on how to import folders into Xcode such that resources within a project have a folder structure that matches the file system.
In this post I will show you two ways to access the imported resources, one by specifying a path, one using an application bundle, both examples will focus on working with images. The code below is an example of building a path to a resource, avoiding a hardcoded path by using index values maintained within the application.
int modelIndex = 2, imageIndex = 5; ... NSString *imageName = [NSString stringWithFormat:@"/Model-%02d/coverflow/CoverFlow-%02d.jpg", modelIndex, imageIndex]; NSLog(@"image: %@", imageName);
The output in the debug console looks as follows, notice the full path to the image:
2009-06-09 08:20:30.732 AppName[25154:20b] image: /Model-02/coverflow/CoverFlow-05.jpg
You can create a UIImageView referencing the file using the code shown below. The method imageNamed: looks for the image in the application’s main bundle if this is the first request for the image. If the image has been loaded previously, the image is retrieved from the system maintained internal cache.
UIImageView *wallpaper = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
There are times when you will need to reference resources by working with the main application bundle. Following on the same theme of working with images, caching of images is not always a good idea (see the post Images and Caching for more information). In this case, using the method imageWithContentsOfFile: is a better option. However, there is one difference we need to account for when using this method, namely, imageWithContentsOfFile: loads image data from a specified path, however, it will not look in the application bundle by default.
Creating the proper path can be as simple as:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Splash" ofType:@"jpg" inDirectory:@""]; UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];
In order for imageWithContentsOfFile: to work with images imported from the file system as I demonstrated in Part 1, we need to specify the full path to the resource, here’s how that code might look:
NSString *dir = [NSString stringWithFormat:@"Model-%02d/coverflow", modelIndex]; NSString *file = [NSString stringWithFormat:@"CoverFlow-%02d", imageIndex]; NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:@"jpg" inDirectory:dir]; UIImageView *wallpaper = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];
Working with files directly imported from the file system can be quite useful, even more so when there are many files to manage within an application. However, you will need to keep in mind how to access the resources, either through a full path or by working with an application bundle.