从documents文件夹运行NSBundle [英] Run NSBundle from the documents folder

查看:88
本文介绍了从documents文件夹运行NSBundle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在iOS上的documents文件夹中使用 NSBundle

Is there any way to use a NSBundle from the documents folder on iOS?

推荐答案

不确定具体问题是什么,但这里是我如何访问我的应用程序的本地文档文件夹(这不是您存储应用程序使用的源文件夹,但是您的应用程序存储的文件夹本地资源)
例如,在我的应用程序中,我用相机拍摄照片并将它们存储到应用程序的本地文件夹,而不是设备相机胶卷,所以要获得我执行此操作的图像数量,在 viewWillAppear 方法使用:

Not sure what the exact question is, either, but here is how I access the local document folder of my app (this is not the documents folder where you store sources your app uses, but the one your app stores local resources) for example, in my app I take pics with the camera and store them to the app's local folder, not the device camera roll, so to get the number of images I do this, in the viewWillAppear method use:

// 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模拟器中实际浏览的那个文件夹)

if you want to know what is in the documents folder (the one you can actually browse in the iOS Simulator


来自〜/ YourUserName / Library / Application Support / iPhone
Simulator / versioOfSimulator / Applications / appFolder / Documents)

via ~/YourUserName/Library/Application Support/iPhone Simulator/versioOfSimulator/Applications/appFolder/Documents)

你会用 NSString * bundleRoot = [[NSBundle mainBundle] bundlePath]; 代替。

希望它可以帮到你,交配!

Hope it helps you, mate!

这篇关于从documents文件夹运行NSBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆