iOS文件系统
当开发者第一次启动app时,iOS操作系统就为此APP创建了一个文件系统,该文件系统下默认有四个,分别是:
Documents:存储用户在操作app时产生的数据,此目录下的数据可以通过iCloud进行同步
Library:用户偏好设置数据,通常和此类NSUserDefaults搭配使用,此目录下的数据可以通过iCloud进行同步
tmp:存放临时数据,此目录下的数据不会通过iCloud进行同步
app包:开发者不会操作此目录,通常是通过此类NSBundle类开获取包内资源,如工程素材
//拿到tmp路径
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"%@",tmpPath);
//获取NSFileManager单例类,用于文件操作
NSFileManager *fileManager = [NSFileManager defaultManager];
// NSString *Imgs = [tmpPath stringByAppendingPathComponent:Imgs];
// NSString *Imgs = [tmpPath stringByAppendingPathComponent:@"Docu"];
//用NSFileManager单例类判断是否为文件夹是否存在
// BOOL isExist = [fileManager fileExistsAtPath:Imgs];
// NSLog(@"%d",isExist);
//找到程序应用的跟路径
NSString *rootPath = NSHomeDirectory();
NSLog(@"%@",rootPath);
//在跟路径后面添加/Documents就是Documents路径
NSString *DicumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//函数获取Document路径。
DicumentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// 拿到library路径
NSString *library = [rootPath stringByAppendingString:@"/library"];
//错误路径
library = [rootPath stringByAppendingString:@"library"];
// NSLog(@"%@",library);
NSLog(@"%d",[fileManager fileExistsAtPath:library]);
//函数获取library路径
library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@",library);
NSError *error;
NSString *myPath = [library stringByAppendingString:@"/lin"];
BOOL isSuccess = [fileManager createDirectoryAtPath:myPath withIntermediateDirectories:YES attributes:nil error:&error];
NSLog(@"%d",isSuccess);
NSArray *imageArray = @[@"http://s0.hao123img.com/res/r/image/2015-08-11/cc1eb723b3afec8d1f65d1edbe794349.jpg",@"http://s0.hao123img.com/res/r/image/2015-08-11/867fdb3ab71b3a31b2c2a763ca84d8ea.jpg",@"http://s0.hao123img.com/res/r/image/2015-08-11/2bf0ad9342cd778452b6e432fc36626d.jpg",@"http://s0.hao123img.com/res/r/image/2015-08-11/608a6bde897e66560752247706452dce.jpg",@"nil"];
int i = 0;
while (i < imageArray.count) {
NSString *imageString = [imageArray objectAtIndex:i];
NSString *imagePath = [myPath stringByAppendingString:[imageString lastPathComponent]];
if ([fileManager fileExistsAtPath:imagePath]) {
NSLog(@"已经下载!");
}else {
NSString *urlString = [imageString stringByAddingPercentEscapesUsingEncoding:4];
NSURL *url = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:url];
if (imageData !=nil) {
if ([fileManager createFileAtPath:imagePath contents:imageData attributes:nil]) {
NSLog(@"下载成功");
}else {
NSLog(@"shibai");
}
}
NSLog(@"%@",NSHomeDirectory());
}
i++;
}