How to get file path in iPhone app

后端 未结 5 2089
独厮守ぢ
独厮守ぢ 2020-12-12 22:22

I have a problem accessing my files in my app.

I am currently using

//Directly from TileMap example from WWDC2010
NSString *tileDirectory = [[[NSBun         


        
相关标签:
5条回答
  • 2020-12-12 22:51

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

    Here is an example:

    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"your_file_name" 
                                                ofType:@"the_file_extension"];
    
    0 讨论(0)
  • 2020-12-12 22:51

    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.

    0 讨论(0)
  • 2020-12-12 22:58

    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);
    
    0 讨论(0)
  • 2020-12-12 23:05

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

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

    It will give you a proper URL ref.

    0 讨论(0)
  • 2020-12-12 23:07

    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.

    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.

    0 讨论(0)
提交回复
热议问题