从文档文件夹运行NSBundle

有没有办法从iOS上的文档文件夹使用NSBundle

不知道确切的问题是什么,但是这里是我如何访问我的应用程序的本地文档文件夹(这不是你的应用程序使用的存储源的文档文件夹,而是应用程序存储本地资源的文档文件夹)在我的应用程序中,我使用相机拍摄照片,并将其存储到应用程序的本地文件夹,而不是设备相机胶卷,因此要获取我在viewWillAppear方法中使用的图像数量:

 // create the route of localDocumentsFolder NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //first use the local documents folder NSString *docsPath = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()]; //then use its bundle, indicating its path NSString *bundleRoot = [[NSBundle bundleWithPath:docsPath] bundlePath]; //then get its content NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil]; // this counts the total of jpg images contained in the local document folder of the app NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.JPG'"]]; // in console tell me how many jpg do I have NSLog(@"numero de fotos en total: %i", [onlyJPGs count]); // --------------- 

如果您想知道文档文件夹中的内容(您可以在iOS模拟器中实际浏览的内容)

通过〜/ YourUserName / Library / Application Support / iPhone Simulator / versioOfSimulator / Applications / appFolder / Documents)

你可以使用NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; 代替。

希望它可以帮助你,队友!

我不完全确定你在做什么,但是从应用程序包中使用文件的一般方法是将其复制到文档目录中,如下所示:

  1. 检查(首次启动,启动或根据需要)在文档目录中存在该文件。

  2. 如果不存在,请将文件的“安装”版本从软件包复制到文档目录中。

根据一些示例代码,我有一个方法,我用于这样的目的如下:

 - (BOOL)copyFromBundle:(NSString *)fileName { BOOL copySucceeded = NO; // Get our document path. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentPath = [searchPaths objectAtIndex:0]; // Get the full path to our file. NSString *filePath = [documentPath stringByAppendingPathComponent:fileName]; NSLog(@"copyFromBundle - checking for presence of \"%@\"...", fileName); // Get a file manager NSFileManager *fileManager = [NSFileManager defaultManager]; // Does the database already exist? (If not, copy it from our bundle) if(![fileManager fileExistsAtPath:filePath]) { // Get the bundle location NSString *bundleDBPath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil]; // Copy the DB to our document directory. copySucceeded = [fileManager copyItemAtPath:bundleDBPath toPath:filePath error:nil]; if(!copySucceeded) { NSLog(@"copyFromBundle - Unable to copy \"%@\" to document directory.", fileName); } else { NSLog(@"copyFromBundle - Succesfully copied \"%@\" to document directory.", fileName); } } else { NSLog(@"copyFromBundle - \"%@\" already exists in document directory - ignoring.", fileName); } return copySucceeded; } 

这将检查文档目录中是否存在指定的文件,如果文件不存在,则从文件夹复制该文件。