简体   繁体   English

iPhone SDK:主包内的子文件夹

[英]iPhone SDK: subFolders inside the main bundle

in the current project I have a number of folders, with subfolders, and these contain images: 01.png, 02.png. 在当前项目中,我有许多文件夹,包含子文件夹,其中包含图像:01.png,02.png。

Folder1/FolderA/f1.png Folder1/FolderB/F1.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. 当我编译应用程序时,我查看了.app并注意到所有图像都放在顶层,没有子文件夹。

So clearly when trying to load the image this doesn't work: 因此在尝试加载图像时这显然不起作用:

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

But even more strangely, when loading image "f1", the image actually loads "F1" 但更奇怪的是,当加载图像“f1”时,图像实际加载“F1”

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"f1.png"]]; 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. 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. 要在.app包中创建子文件夹,您应该选中“为任何添加的文件夹创建文件夹引用”选项,而不是默认的“为任何添加的文件夹递归创建组”选项。现在在XCode中,导入的文件夹显示为蓝色而不是黄色。 Build and go and you should see folders in your .app file. 构建并继续,您应该在.app文件中看到文件夹。

Just wanted to add to Mugunth's answer, to follow up on part of the original question which was trying to use: 只是想添加到Mugunth的答案,跟进试图使用的原始问题的一部分:

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. 上面的'pathForResouce'调用将在模拟器上运行,但不适用于实际的iPhone(将返回nil),因为它们似乎不能识别方法的'inDirectory'部分中的子文件夹。 They do recognize them in the 'pathForResource' part, though. 但是,他们确实在'pathForResource'部分识别它们。 So the way of rephrasing the above so that it works on iPhones is: 因此,重新描述上述内容使其适用于iPhone的方式是:

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 我得到的结果是:/ Users / iosif / Library / Application Support / iPhone Simulator / 6.1 / Applications / 33FB4F79-999C-4455-A092-906A75226CDB / Arithmetics.app / About.png

First of all the folders you create in Xcode are simply organizational structures without any counterpart on the filesystem level. 首先,您在Xcode中创建的文件夹只是组织结构,在文件系统级别上没有任何对应物。 In other words all folders except for the "Classes" folder gets flatten out at the filesystem level. 换句话说,除了“Classes”文件夹之外的所有文件夹在文件系统级别都变得扁平化。 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. 因此,即使您将图像文件放在xcode中的以下位置,它仍将存在于文件系统的顶层:f1 / f2 / f3 / f4 / f5 / image.png。 Thus, in pathForResource method, you should not include the inDirectory argument. 因此,在pathForResource方法中,不应包含inDirectory参数。

As for the second issue, mac osx doesn't recognize case-sensitive file names. 至于第二个问题,mac osx不识别区分大小写的文件名。 Therefore, f1 and F1 are equivalent to mac osx and will reference the same file. 因此,f1和F1等同于mac osx并将引用相同的文件。 You can easily see this by executing the following 2 commands at a terminal session: 您可以通过在终端会话中执行以下2个命令来轻松查看:

touch f
touch F

you'll notice that only 1 file exists after this: namely f. 你会注意到在此之后只存在一个文件:即f。 If you reverse the 2 commands, you still get one file, but it is named F. 如果你反转2个命令,你仍然会得到一个文件,但它被命名为F.

ennuikiller is right. ennuikiller是对的。 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. 我认为您可以通过Finder在子文件夹中整理图像,然后通过右键单击图像并选择“获取信息”选项刷新XCode中的图像位置。 Then set the new directory. 然后设置新目录。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM