//这里是app的Bundle的地址,获取的路径是你程序的安装路径下的资源文件位置
NSString *appPath =[[NSBundle mainBundle] bundlePath]; NSLog(@"%@",appPath);
//这个方法能获取一个数组,数组里面印象中是2个路径,但是IOS只有一个,在IOS中使用的时候,直接用数组中的第一个即可,第一个参数为你要的路径的类型,此处为获取Document文件夹
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask , YES);
NSString *Path =[paths objectAtIndex:0] NSLog(@"%@",Path);
//同上,但是获取的是Library文件夹
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *Path2 =[paths2 objectAtIndex:0] NSLog(@"%@",Path2);
//同上,但是获取的是Caches文件夹
NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *Path3 =[paths3 objectAtIndex:0] NSLog(@"%@",Path3);
//获取tmp文件夹路径
NSString * tmpDir = NSTemporaryDirectory(); NSLog(@"%@",tmpDir);
//获取应用程序目录路径(里面有Document,Library和temp文件夹
NSString * homeDir = NSHomeDirectory(); NSLog(@"%@",homeDir);
//获取项目中文件的目录
NSString * fileDir = [[NSBundle mainBundle] pathForResource:@"pic1" ofType:@"jpg"]; NSLog(@"%@",fileDir);
fileDir = [[NSBundle mainBundle] pathForResource:@"pic2.jpg" ofType:nil]; NSLog(@"%@",fileDir);
//工程下黄色目录是虚拟目录.蓝色目录是真实目录
fileDir = [[NSBundle mainBundle] pathForResource:@"pic3.jpg" ofType:nil inDirectory:@"img"];
NSLog(@"%@",fileDir);
fileDir = [[NSBundle mainBundle] pathForResource:@"img/pic4.jpg" ofType:nil inDirectory:nil];
NSLog(@"%@",fileDir);
OC中获取系相应目录的几种方法
最新推荐文章于 2022-03-17 22:27:34 发布