1.将文件写入沙盒中
#pragma mark - 保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
// 获取沙盒目录
NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/ImageCaches"] stringByAppendingPathComponent:imageName];
//NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Caches"] stringByAppendingPathComponent:imageName];
// 将图片写入文件
[imageData writeToFile:fullPath atomically:NO];
[_filePathArray addObject:fullPath];
}
2. 在Documents文件下创建一个ImageCaches的文件目录
// 创建文件夹
- (void)createFileDir
{
NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/ImageCaches", NSHomeDirectory()];
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) )
{
[fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
}
}
- (void)deleteAllFile
{
NSString *extension = @"png";
NSFileManager *fileManager = [NSFileManager defaultManager];
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentsDirectory = [NSString stringWithFormat:@"%@/Documents/ImageCaches", NSHomeDirectory()];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while ((filename = [e nextObject]))
{
if ([[filename pathExtension] isEqualToString:extension])
{
[fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
}
}
}
4. 读出ImageCaches下的所有文件
- (void)getAllFileName
{
//NSString *path=@"System/Library/"; // 要列出来的目录
NSString *documentsDirectory = [NSString stringWithFormat:@"%@/Documents/ImageCaches", NSHomeDirectory()];
NSFileManager *myFileManager=[NSFileManager defaultManager];
NSDirectoryEnumerator *myDirectoryEnumerator;
myDirectoryEnumerator=[myFileManager enumeratorAtPath:documentsDirectory];
//列举目录内容,可以遍历子目录
NSLog(@"用enumeratorAtPath:显示目录%@的内容:",documentsDirectory);
while((documentsDirectory = [myDirectoryEnumerator nextObject])!=nil)
{
NSLog(@"%@",documentsDirectory);
}
}
5.返回指定文件夹下的全部文件名称
+ (NSArray*) allFilesAtPath:(NSString*)dirName
{
NSString *dirString = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(),dirName];
NSMutableArray* array = [NSMutableArray arrayWithCapacity:10];
NSFileManager* fileMgr = [NSFileManager defaultManager];
NSArray* tempArray = [fileMgr contentsOfDirectoryAtPath:dirString error:nil];
for (NSString* fileName in tempArray)
{
BOOL flag = YES;
NSString* fullPath = [dirString stringByAppendingPathComponent:fileName];
if ([fileMgr fileExistsAtPath:fullPath isDirectory:&flag])
{
if (!flag)
{
//[array addObject:fullPath];
[array addObject:fileName];
}
}
}
return array;
}
6.根据文件名删除文件
+ (void)deleteFileByName:(NSString*)fileNameStr FileDir:(NSString*)FileDirStr
{
//删除文件夹及文件级内的文件:
NSString *fileName = [NSString stringWithFormat:@"%@/Documents/%@/%@", NSHomeDirectory(),FileDirStr,fileNameStr];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager removeItemAtPath:fileName error:nil])
{
NSLog(@"文件删除成功");
}
}
测试代码:遍历文件中所有的文件名,然后删除第一个文件
NSArray *tfTempArray = [NSArray arrayWithArray:[TFGoodsFileManager allFilesAtPath:FileDirStr]];
for (NSString *tffilePath in tfTempArray)
{
CHDebugLog(@"------%@----",tffilePath);
}
[TFGoodsFileManager deleteFileByName:tfTempArray[0] FileDir:FileDirStr];