標籤:style blog io color os sp 檔案 div on
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.png21 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);
IOS在Document目錄下建立檔案夾、儲存、讀取、以及刪除檔案