ios 如何在iPhone应用程序中获取文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6387915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:24:08  来源:igfitidea点击:

How to get file path in iPhone app

iphoneiosxcodepath

提问by LouwHopley

I have a problem accessing my files in my app.

我在我的应用程序中访问我的文件时遇到问题。

I am currently using

我目前正在使用

//Directly from TileMap example from WWDC2010
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] 
                           stringByAppendingPathComponent:@"Tiles"];

to access my tiles for my MKOverlay. This gives me this directory

为我的MKOverlay. 这给了我这个目录

/Users/xxxx/Library/Application Support/iPhone Simulator/4.2/Applications/9D62025C-C53B-472C-8309-xxxx/xxxx.app/Tiles

/Users/xxxx/Library/Application Support/iPhone Simulator/4.2/Applications/9D62025C-C53B-472C-8309-xxxx/xxxx.app/Tiles

The x's is only for privacy reasons

x 仅出于隐私原因

I have my tiles in a folder called Tilesin the root of my application which is in Xcode in a group called Tileswhich is in directly in the Resourcesgroup.

我的磁贴位于Tiles我的应用程序根目录中的文件夹中,该文件夹位于 Xcode 中的一个组Tiles中,该Resources组直接位于该组中。

When I run my app, I get a simple error saying that it could not find my tiles at the generated directory (the one quote above) If I replace that piece of code and make it:

当我运行我的应用程序时,我收到一个简单的错误,说它在生成的目录中找不到我的磁贴(上面的一个引用)如果我替换那段代码并使它的:

NSString *tileDirectory = @"/Users/xxxx/Documents/xxxx/Tiles";

Then my app works fine. This is obviously because it finds my tiles in its direct location on my Mac. This is fine for testing, but I need it to work on my iPhone/iPad.

然后我的应用程序运行良好。这显然是因为它在我的 Mac 上的直接位置找到了我的磁贴。这对于测试来说很好,但我需要它在我的 iPhone/iPad 上工作。

This problem might be occurring due to:

出现此问题的原因可能是:

  • The generated directory is incorrect.
  • The tile images aren't getting included in the builded .appfile.
  • 生成的目录不正确。
  • 平铺图像未包含在构建的.app文件中。

Either way, I have no clue of what to do to solve it.

无论哪种方式,我都不知道该怎么做才能解决它。

How can I solve this problem?

我怎么解决这个问题?



[EDIT]

[编辑]

I changed that piece of code to:

我把那段代码改成:

NSString *tileDirectory = [[NSBundle mainBundle] resourcePath];

Now it works in simulator, because all files are in the apps root folder and I don't ask for it to enter another directory called "Tiles".

现在它在模拟器中工作,因为所有文件都在应用程序根文件夹中,我不要求它进入另一个名为“Tiles”的目录。

This runs with no error on the simulator, but when on my iPhone it gives the original error (just a different file path but also ending with /xxxx.app

这在模拟器上运行没有错误,但是在我的 iPhone 上它给出了原始错误(只是一个不同的文件路径,但也以 /xxxx.app

How can I ensure a directory in my app file such as xxxx.app/Tiles- TileMap does this.

如何确保我的应用程序文件中的目录,例如xxxx.app/Tiles- TileMap 执行此操作。

采纳答案by LuckyLuke

Remember that the "folders/groups" you make in xcode, those which are yellowish are not reflected as real folders in your iPhone app. They are just there to structure your XCode project. You can nest as many yellow group as you want and they still only serve the purpose of organizing code in XCode.

请记住,您在 xcode 中创建的“文件夹/组”,那些黄色的不会在您的 iPhone 应用程序中反映为真正的文件夹。他们只是在那里构建您的 XCode 项目。您可以根据需要嵌套任意数量的黄色组,但它们仍然仅用于在 XCode 中组织代码的目的。

EDIT

编辑

Make a folder outside of XCode then drag it over, and select "Create folder references for any added folders" instead of "Create groups for any added folders" in the popup.

在 XCode 之外创建一个文件夹,然后将其拖过来,然后在弹出窗口中选择“为任何添加的文件夹创建文件夹引用”而不是“为任何添加的文件夹创建组”。

回答by Jue Wang

Since it is your files in your app bundle, I think you can use pathForResource:ofType:to get the full pathname of your file.

由于它是您的应用程序包中的文件,我认为您可以使用它pathForResource:ofType:来获取文件的完整路径名。

Here is an example:

下面是一个例子:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"your_file_name" 
                                            ofType:@"the_file_extension"];

回答by Joe

If your tiles are not in your bundle, either copied from the bundle or downloaded from the internet you can get the directory like this

如果您的瓷砖不在您的捆绑包中,无论是从捆绑包中复制还是从互联网下载,您都可以获得这样的目录

NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *tileDirectory = [documentdir stringByAppendingPathComponent:@"xxxx/Tiles"];
NSLog(@"Tile Directory: %@", tileDirectory);

回答by Dylan Rosario

You need to use the URL for the link, such as this:

您需要使用链接的 URL,例如:

NSURL *path = [[NSBundle mainBundle] URLForResource:@"imagename" withExtension:@"jpg"];

It will give you a proper URL ref.

它会给你一个正确的 URL 引用。

回答by Rahul Vyas

You need to add your tiles into your resource bundle. I mean add all those files to your project make sure to copy all files to project directory option checked.

您需要将您的磁贴添加到您的资源包中。我的意思是将所有这些文件添加到您的项目中,确保将所有文件复制到项目目录选项已选中。