创建文件夹
//-- to select path from image--//
AppDelegate *del = (AppDelegate*)[[UIApplication sharedApplication]delegate];
NSString *dataPath=[del.databasePath copy];
NSString *Documentpath=[del.path_Folder_Document copy];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
dataPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:@"/Images"];
Pathforimage=dataPath;
如果不存在则添加文件夹
if (![[NSFileManager defaultManager] createDirectoryAtPath:Pathforimage withIntermediateDirectories:NO attributes:nil error:nil])
{
//NSLog(@"directory... /Images already exist..");
}
else
{
//NSLog(@"directory created!");
}
NSString *pathforAudios=[NSString stringWithFormat:@"%@/Images_Audio",Documentpath];
if (![[NSFileManager defaultManager] createDirectoryAtPath:pathforAudios withIntermediateDirectories:NO attributes:nil error:nil])
{
//NSLog(@"directory... /Images already exist..");
}
else
{
//NSLog(@"directory created!");
}
//-- end of path --//
将图像保存到文件夹
//Save Image to Documents Directory
NSString *imagePath = [Pathforimage stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",Img_File_Name]];
[UIImageJPEGRepresentation(image, 0.1) writeToFile:imagePath atomically:YES];
imagePath=nil;
image=nil;
删除文件/文件夹
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(@"files =%@",files);
NSString *Images_Folder = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:@"/Images"];
NSArray *fileArray = [fileMgr contentsOfDirectoryAtPath:Images_Folder error:nil];
for (NSString *filename in fileArray)
{
NSLog(@"Images =%@",filename);
[fileMgr removeItemAtPath:[Images_Folder stringByAppendingPathComponent:filename] error:NULL];
}
NSString *Images_Audio_Folder = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:@"/Images_Audio"];
NSArray *fileArray1 = [fileMgr contentsOfDirectoryAtPath:Images_Audio_Folder error:nil];
for (NSString *filename in fileArray1)
{
NSLog(@"Images_Audio =%@",filename);
[fileMgr removeItemAtPath:[Images_Audio_Folder stringByAppendingPathComponent:filename] error:NULL];
}