iPhone SDK:主包内的子文件夹

发布于 2024-08-24 23:11:48 字数 527 浏览 0 评论 0原文

在当前项目中,我有许多带有子文件夹的文件夹,其中包含图像:01.png、02.png。

文件夹1/文件夹A/f1.png Folder1/FolderB/F1.png

当我编译应用程序时,我查看了 .app 内部,发现所有图像都放置在顶层,没有子文件夹。

很明显,当尝试加载图像时,这不起作用:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"f1" 类型:@"png" inDirectory:@"文件夹1/文件夹A"];

但更奇怪的是,当加载图像“f1”时,图像实际上加载了“F1”

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

有人对如何解决这个问题有想法吗?

是否有编译选项可以在应用程序包中创建文件夹?

TIA。

in the current project I have a number of folders, with subfolders, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

波浪屿的海角声 2024-08-31 23:11:48

要在 .app 包内创建子文件夹,您应该选中“为任何添加的文件夹创建文件夹引用”选项,而不是默认的“为任何添加的文件夹递归创建组”
现在在 XCode 中,导入的文件夹显示为蓝色而不是黄色。构建并运行,您应该会在 .app 文件中看到文件夹。

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.

贪恋 2024-08-31 23:11:48

首先,您在 Xcode 中创建的文件夹只是简单的组织结构,在文件系统级别上没有任何对应项。换句话说,除了“Classes”文件夹之外的所有文件夹都会在文件系统级别上变平。因此,即使您将图像文件放在 xcode 中的以下位置,它仍然会存在于文件系统的顶层:f1/f2/f3/f4/f5/image.png。因此,在 pathForResource 方法中,不应包含 inDirectory 参数。

至于第二个问题,mac osx 不识别区分大小写的文件名。因此,f1和F1相当于mac osx,并且将引用相同的文件。您可以通过在终端会话中执行以下 2 个命令轻松地看到这一点:

touch f
touch F

您会注意到此后仅存在 1 个文件:即 f.如果你颠倒这 2 个命令,你仍然得到一个文件,但它被命名为 F。

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.

左耳近心 2024-08-31 23:11:48

无聊杀手是对的。我认为您可以通过子文件夹中的 Finder 组织图像,然后通过右键单击图像并选择“获取信息”选项来刷新 XCode 中的图像位置。然后设置新目录。

干杯。

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.

夏天碎花小短裙 2024-08-31 23:11:48

只是想添加 Mugunth 的答案,以跟进尝试使用的原始问题的一部分:

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

... 访问使用文件夹引用添加的文件。上面的“pathForResouce”调用将在模拟器上运行,但不适用于实际的 iPhone(将返回 nil),因为它们似乎无法识别该方法的“inDirectory”部分中的子文件夹。不过,他们确实在“pathForResource”部分中识别了它们。因此,重新表述上述内容以使其适用于 iPhone 的方法是:

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

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"];
绝情姑娘 2024-08-31 23:11:48

我已遵循您的答案,但它接缝所有文件都存储在根目录上。如果我使用下面的代码来获取完整路径,

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

则会得到结果:/Users/iosif/Library/Application Support/iPhone Simulator/6.1/Applications/33FB4F79-999C-4455-A092-906A75226CDB/Arithmetics.app/About。 PNG

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文