原]NSBundle--獲取文件路徑
2012-6-29閱讀16207 評(píng)論0
之前在初始化一個(gè)類的時(shí)候:TestViewController *viewcontroller=[[TestViewController alloc]initWithNibName:@"TestViewController" bundle:[NSBundle mainBundle]];不是很明白:[NSBundle mainBundle]的意思鸟缕。后來(lái)查閱資料后知道了它的作用盗冷,如下:
bundle是一個(gè)目錄,其中包含了程序會(huì)使用到的資源. 這些資源包含了如圖像,聲音,編譯好的代碼,nib文件(用戶也會(huì)把bundle稱為plug-in). 對(duì)應(yīng)bundle,cocoa提供了類NSBundle.我們的程序是一個(gè)bundle. 在Finder中,一個(gè)應(yīng)用程序看上去和其他文件沒有什么區(qū)別. 但是實(shí)際上它是一個(gè)包含了nib文件,編譯代碼,以及其他資源的目錄. 我們把這個(gè)目錄叫做程序的main bundle。
通過使用下面的方法得到程序的main bundle
NSBundle *myBundle = [NSBundle mainBundle];
一般我們通過這種方法來(lái)得到bundle.如果你需要其他目錄的資源,可以指定路徑來(lái)取得bundle
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];
一旦我們有了NSBundle 對(duì)象,那么就可以訪問其中的資源了
NSBundle束弄屡,是一種特定的文件類型窍育,其中的內(nèi)容遵循特定的結(jié)構(gòu)缀蹄。
NSBundle的一個(gè)主要作用是 獲取Resources文件夾中的資源在辆。
在編程中使用[NSData dataWithContentOfFile:@"foo"]的時(shí)候规揪,總是無(wú)法讀取正確的文件內(nèi)容。而使用[NSData dataWithContentOfFile:[[NSBundle mainBundle] pathForResource:@”foo” ofType:@”"]的時(shí)候就可以。
因?yàn)楫?dāng)使用相對(duì)路徑的時(shí)候瓣蛀,其實(shí)他相對(duì)的當(dāng)前目錄并不是程序運(yùn)行的目錄,而是“/”雷厂。只有使用[NSBundle mainBundle]來(lái)生成的路徑才是文件真正的路徑惋增。
在此記錄一下:在以后的開發(fā)中不直接使用任何相對(duì)路徑,而是使用經(jīng)過計(jì)算以后的絕對(duì)路徑改鲫。
一.獲取圖片
-
NSString *path = [[NSBuddle mainBuddle] pathForResource:@"resourceName" oftype@"resourceType"];
UIImage *image = [[UIImage imageWithContentsOfFile:path];
UIImage *image = [UIImage imageNamed:@"imageName"];
二.獲取plist文件
NSArray *array =[[NSArrayalloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"name"ofType:@"plist"]];
NSDictionary *dict=[arrayobjectAtIndex:index];//將plist文件中的內(nèi)容轉(zhuǎn)換成字典
(????ω????)
NSBundle介紹
閱讀:232302011-09-22 17:51
標(biāo)簽:雜談
bundle是一個(gè)目錄,其中包含了程序會(huì)使用到的資源. 這些資源包含了如圖像,聲音,編譯好的代碼,nib文件(用戶也會(huì)把bundle稱為plug-in). 對(duì)應(yīng)bundle,cocoa提供了類NSBundle.
我們的程序是一個(gè)bundle. 在Finder中,一個(gè)應(yīng)用程序看上去和其他文件沒有什么區(qū)別. 但是實(shí)際上它是一個(gè)包含了nib文件,編譯代碼,以及其他資源的目錄. 我們把這個(gè)目錄叫做程序的main bundle
bundle中的有些資源可以本地化.例如,對(duì)于foo.nib,我們可以有兩個(gè)版本: 一個(gè)針對(duì)英語(yǔ)用戶,一個(gè)針對(duì)法語(yǔ)用戶. 在bundle中就會(huì)有兩個(gè)子目錄:English.lproj和French.lproj,我們把各自版本的foo.nib文件放到其中. 當(dāng)程序需要加載foo.nib文件時(shí),bundle會(huì)自動(dòng)根據(jù)所設(shè)置的語(yǔ)言來(lái)加載. 我們會(huì)在16章再詳細(xì)討論本地化
通過使用下面的方法得到程序的main bundle
NSBundle *myBundle = [NSBundle mainBundle];
一般我們通過這種方法來(lái)得到bundle.如果你需要其他目錄的資源,可以指定路徑來(lái)取得bundle
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@'~/.myApp/Good.bundle'];
一旦我們有了NSBundle 對(duì)象,那么就可以訪問其中的資源了
// Extension is optional
NSString *path = [goodBundle pathForImageResource:@'Mom'];
NSImage *momPhoto = [[NSImage alloc] initWithContentsOfFile:path];
bundle中可以包含一個(gè)庫(kù). 如果我們從庫(kù)得到一個(gè)class, bundle會(huì)連接庫(kù),并查找該類:
Class newClass = [goodBundle classNamed:@'Rover'];
id newInstance = [[newClass alloc] init];
如果不知到class名,也可以通過查找主要類來(lái)取得
Class aClass = [goodBundle principalClass];
id anInstance = [[aClass alloc] init];
可以看到, NSBundle有很多的用途.在這當(dāng)中, NSBundle負(fù)責(zé)(在后臺(tái))加載nib文件. 我們也可以不通過NSWindowController來(lái)加載nib文件, 直接使用NSBundle:
BOOL successful = [NSBundle loadNibNamed:@'About' owner:someObject];
注意噢, 我們指定了一個(gè)對(duì)象someObject作為nib的File's Owner
使用initWithContentsOfFile時(shí)诈皿,文件路徑的寫法 使用initWithContentsOfFile方法可以通過讀取一個(gè)文件的內(nèi)容來(lái)初始化對(duì)象。 但文件的路徑應(yīng)該怎么確定呢像棘? 可以使用NSBundle的對(duì)象來(lái)獲取稽亏。 例如當(dāng)前程序所在目錄下有個(gè)文件re.xml,我們要將該文件的內(nèi)容做為NSData的數(shù)據(jù)源來(lái)初始化一個(gè)NSData對(duì)象缕题,可以用下面的方法來(lái)實(shí)現(xiàn):
NSString *filePath = [[NSBundle mainBundle] pathForResouse:@'re' ofType:@'xml']; NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
讀取plist中的內(nèi)容:
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@'Data' ofType:@'plist']; self.data = [NSArray arrayWithContentsOfFile:dataPath];
刪除本地文件
NSString * thePath=[self getUserDocumentDirectoryPath];
NSMutableString * fullPath=[[[NSMutableString alloc]init]autorelease];
[fullPath appendString:thePath];
NSString * idString=[idArray objectAtIndex:indexPath.row];
NSString * coverName=[NSString stringWithFormat:@'/%@.jpg',idString];
[fullPath appendString:coverName];
NSFileManager *defaultManager;
defaultManager = [NSFileManager defaultManager];
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error BOOL boolValue=[defaultManager removeItemAtPath: fullPath error: nil];
if (boolValue) {
NSLog(@'remove cover image ok');
} - (NSString)getUserDocumentDirectoryPath {
NSArray array = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
if([array count])
return [array objectAtIndex: 0];
else return @'';
}