Bundles and file access

后端 未结 3 1074
南方客
南方客 2021-02-06 17:35

I\'ve adding a series of folders to my project (using the Add existing file option). This results in the folder contents being shown in Xcode as a blue folder.

The follo

相关标签:
3条回答
  • 2021-02-06 18:15

    To access and store files on the iPhone I recommend reading this.

    Erica Sadun has also written a nice shorty about accessing file within a app bundle.

    Note that how files are organized in Xcode does not show how they will end up in the app bundle. The best way is to select the app bundle in the Finder and CTRL-click and select Show Package Content.

    0 讨论(0)
  • 2021-02-06 18:24

    The problem with your imageWithContentsOfFile code is that you're assuming that / is the root of the bundle, when it's the root of the FS!

    So, pathForResource:ofType:inDirectory: is probably the easiest way to get the path you need.

    Otherwise, you can get the root of the bundle and prefix that to your paths.

    0 讨论(0)
  • 2021-02-06 18:37

    Okay, I figured out one way in code to get what I needed:

      NSString *dir = [NSString stringWithFormat:@"File-03/images", imageIndex];
      NSString *file = [NSString stringWithFormat:@"image-%02d", imageIndex]; 
      NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:@"jpg" inDirectory:dir];
    
      return = [UIImage imageWithContentsOfFile:path];
    

    The trick was to inDirectory in the bundle call.

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