IOS在Document目录下创建文件夹、保存、读取、以及删除文件

 1     //  在Documents目录下创建一个名为LaunchImage的文件夹

 2     NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"LaunchImage"];

 3     NSLog(@"%@",path);

 4     

 5     NSFileManager *fileManager = [NSFileManager defaultManager];

 6     BOOL isDir = FALSE;

 7     BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];

 8     if(!(isDirExist && isDir))

 9         

10     {

11         BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

12         if(!bCreateDir){

13             NSLog(@"创建文件夹失败!");

14         }

15         NSLog(@"创建文件夹成功,文件路径%@",path);

16     }

17     

18     UIImage *image=[UIImage imageWithContentsOfFile:@"testimage.png"];

19     NSData *data = UIImagePNGRepresentation(image);

20     //将testimage.png保存到LaunchImage文件夹下,新名称为image1.png

21          BOOL isSaved=   [fileManager createFileAtPath:[path stringByAppendingString:@"/image1.png"] contents:data attributes:nil];

22     NSLog(@"图片保存状态:%d",isSaved);

23     

24     //获取保存的图片

25     UIImage *newimage=[UIImage imageWithContentsOfFile:[path stringByAppendingString:@"/image1.png"]];

26    

27 //   删除文件

28      BOOL isDelete=[fileManager removeItemAtPath:path error:nil];

29     NSLog(@"%d",isDelete);

 

你可能感兴趣的:(document)