1.NSFileManager:單例,對文件進行操做atom
[NSFileManager defaultManager]
2.UIImage轉NSDatacode
NSData *imageData = UIImagePNGRepresentation(image);
3.NSHomeDirectory:獲取應用的主路徑圖片
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/pic"];
4.fileExistsAtPath判斷路徑下是否存在這個文件rem
BOOL isExit = [[NSFileManager defaultManager] fileExistsAtPath:path];
5.createFileAtPath:在路徑上建立文件string
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
6.createDirectoryAtPath:在路徑上建立目錄it
[[NSFileManager defaultManager] createDirectoryAtPath:dir2Path withIntermediateDirectories:YES attributes:nil error:nil];
7.將文件轉化爲NSDataio
NSData *data = [NSData dataWithContentsOfFile:path];
8.得到應用的Document目錄ast
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
9.moveItemAtPath:toPath:將第一個路徑上的文件移動到另外一個路徑class
[[NSFileManager defaultManager] moveItemAtPath:path toPath:[dir1Path stringByAppendingPathComponent:@"pic1"] error:&errorMsg];
10.copyItemAtPath:toPath:將第一個路徑上的文件拷貝粘貼到另外一個路徑上file
[[NSFileManager defaultManager] copyItemAtPath:[dir1Path stringByAppendingPathComponent:@"pic1"] toPath:[dir2Path stringByAppendingPathComponent:@"pic2"] error:&errorMsg];
11.removeItemAtPath:刪除文件
[[NSFileManager defaultManager] removeItemAtPath:dir1Path error:nil];
小例子:
@interface ViewController () @property (nonatomic, strong) NSFileManager *fileManager; @end ///Users/a/Library/Developer/CoreSimulator/Devices/4DF9F8F0-E0BD-4EFC-AC49-340DAE285D62/data/Containers/Data/Application/77EA378C-8B16-424E-8607-456EAC48EC7A/Documents/pic @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //獲取系統提供文件管理器 單例 self.fileManager = [NSFileManager defaultManager]; //讀取圖片 UIImage *image = [UIImage imageNamed:@"4.jpg"]; //UIImage -> NSData//二進制 NSData *imageData = UIImagePNGRepresentation(image); // NSLog(@"%@", imageData); //建立文件 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/pic"];//添加的是路徑的一部分 NSLog(@"%@", path); //建立文件 if (![self.fileManager fileExistsAtPath:path]) { //文件不存在 //建立一個空的文件 [self.fileManager createFileAtPath:path contents:nil attributes:nil]; //寫入內容 [imageData writeToFile:path atomically:YES]; } //讀取文件 if ([self.fileManager fileExistsAtPath:path]) { NSData *data = [NSData dataWithContentsOfFile:path]; UIImage *image = [UIImage imageWithData:data]; self.view.backgroundColor = [UIColor colorWithPatternImage:image]; } //建立兩個目錄:目錄1 目錄2 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];//自動尋找可操做的目錄,可是隻有一個 // NSLog(@"%@", dir1Path); //兩個方法不一樣 NSString *dir1Path = [docPath stringByAppendingString:@"/目錄1"]; NSString *dir2Path = [docPath stringByAppendingPathComponent:@"目錄2"]; // NSLog(@"%@, %@", dir1Path, dir2Path); // BOOL *isExist; if (![self.fileManager fileExistsAtPath:dir1Path]) { //不存在,建立 [self.fileManager createDirectoryAtPath:dir1Path withIntermediateDirectories:YES attributes:nil error:nil]; } if (![self.fileManager fileExistsAtPath:dir2Path]) { //不存在,建立 [self.fileManager createDirectoryAtPath:dir2Path withIntermediateDirectories:YES attributes:nil error:nil]; } //將Documents/pic 移動到Documents/目錄1/pic1,移動的時候還要設置文件名 NSError *errorMsg = NULL; [self.fileManager moveItemAtPath:path toPath:[dir1Path stringByAppendingPathComponent:@"pic1"] error:&errorMsg]; if (errorMsg) { //有錯誤信息 NSLog(@"errorMsg:%@", errorMsg); }else{ NSLog(@"move ok"); } //將目錄1/pic1 拷貝copy 到目錄2/pic1 [self.fileManager copyItemAtPath:[dir1Path stringByAppendingPathComponent:@"pic1"] toPath:[dir2Path stringByAppendingPathComponent:@"pic2"] error:&errorMsg]; if (errorMsg) { //有錯誤信息 NSLog(@"errorMsg:%@", errorMsg); }else{ NSLog(@"copy ok"); } //刪除文件 [self.fileManager removeItemAtPath:dir1Path error:nil]; } @end