开发者

iPhone SDK: subFolders inside the main bundle

in the current project I have a number of folders, with subfol开发者_StackOverflow社区ders, and these contain images: 01.png, 02.png.

Folder1/FolderA/f1.png Folder1/FolderB/F1.png

When I compile the app, I looked inside the the .app and noticed that all the images are placed in the top level, with no sub-folders.

So clearly when trying to load the image this doesn't work:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"f1" ofType:@"png" inDirectory:@"Folder1/FolderA"];

But even more strangely, when loading image "f1", the image actually loads "F1"

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"f1.png"]];

Anyone have ideas on how to get around this problem?

Is there a compile option to create the folders in the app bundle?

TIA.


To create the subfolders inside the .app bundle, you should check the option "Create folder references for any added folders" rather than the default "Recursively create groups for any added folders" Now in XCode, your imported folder appears blue rather than yellow. Build and go and you should see folders in your .app file.


First of all the folders you create in Xcode are simply organizational structures without any counterpart on the filesystem level. In other words all folders except for the "Classes" folder gets flatten out at the filesystem level. Therefore, even if you put your image file in the following location within xcode, it would still exist at the top-level in the filesystem: f1/f2/f3/f4/f5/image.png. Thus, in pathForResource method, you should not include the inDirectory argument.

As for the second issue, mac osx doesn't recognize case-sensitive file names. Therefore, f1 and F1 are equivalent to mac osx and will reference the same file. You can easily see this by executing the following 2 commands at a terminal session:

touch f
touch F

you'll notice that only 1 file exists after this: namely f. If you reverse the 2 commands, you still get one file, but it is named F.


ennuikiller is right. I think you can organize your images via Finder in subfolder and then refresh the image location in XCode by right clicking your image and selecting "Get Info" option. Then set the new directory.

Cheers.


Just wanted to add to Mugunth's answer, to follow up on part of the original question which was trying to use:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"f1" ofType:@"png" inDirectory:@"Folder1/FolderA"];

... to access files added using folder references. The above 'pathForResouce' call will work on simulators but not on actual iPhones (nil will be returned), because they don't seem to recognize subfolders in the 'inDirectory' part of the method. They do recognize them in the 'pathForResource' part, though. So the way of rephrasing the above so that it works on iPhones is:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"FolderA/f1" ofType:@"png" inDirectory:@"Folder1"];


I've followed your answer but it seams all files are stored flat on the root. If I use the code below to get the full path

CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef url = CFBundleCopyResourceURL(mainBundle, CFSTR("About"), CFSTR("png"), NULL);
UInt8 filePath[PATH_MAX];
CFURLGetFileSystemRepresentation(url, true, filePath, sizeof(filePath));

I get as a result: /Users/iosif/Library/Application Support/iPhone Simulator/6.1/Applications/33FB4F79-999C-4455-A092-906A75226CDB/Arithmetics.app/About.png

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜