iOS 文件寫入沙盒與刪除文件

// 把 圖片 寫入 沙盒

-(void)photoFile {

//此處首先指定了圖片存取路徑(默認(rèn)寫到應(yīng)用程序沙盒 中)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

//并給文件起個(gè)文件名

NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin"];

BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];

if (blHave) {

NSLog(@"already have");

return ;

}

//此處的方法是將圖片寫到Documents文件中 如果寫入成功會(huì)彈出一個(gè)警告框,提示圖片保存成功

NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"pin" ofType:@"png"];

NSData *data = [NSData dataWithContentsOfFile:strPathOld];

BOOL result = [data writeToFile:uniquePath atomically:YES];

if (result) {

NSLog(@"success");

}else {

NSLog(@"no success");}

}

// 刪除沙盒里的文件

-(void)deleteFile {

NSFileManager* fileManager=[NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

//文件名

NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];

BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];

if (!blHave) {

NSLog(@"no? have");

return ;

}else {

NSLog(@" have");

BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];

if (blDele) {

NSLog(@"dele success");

}else {

NSLog(@"dele fail");

}}}

// 圖片

UIImage *img = [UIImage imageNamed:@"1.png"];

NSData* data = UIImagePNGRepresentation(img);

//向沙盒里 寫入文件夾,并向文件夾里 寫入東西

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *folder = [document stringByAppendingPathComponent:@"folder"];

NSString *filePath = [folder stringByAppendingPathComponent:@"test.png"];

if (![fileManager fileExistsAtPath:folder]) {

BOOL blCreateFolder= [fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:NULL];

if (blCreateFolder) {

NSLog(@" folder success");

}else {

NSLog(@" folder fial");

}

}else {

NSLog(@" 沙盒文件已經(jīng)存在");

}

if (![fileManager fileExistsAtPath:filePath]) {

NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];

NSData *data = [NSData dataWithContentsOfFile:strPathOld];

BOOL result = [data writeToFile:filePath atomically:YES];

if (result) {

NSLog(@"success");

}else {

NSLog(@"no success");}}

//得到沙盒文件夾 下的所有文件

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *document=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *folder =[document stringByAppendingPathComponent:@"folder"];

NSArray *fileList ;

fileList =[fileManager contentsOfDirectoryAtPath:folder error:NULL];

for (NSString *file in fileList) {

NSLog(@"file=%@",file);

NSString *path =[folder stringByAppendingPathComponent:file];

NSLog(@"得到的路徑=%@",path);

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言: 出于安全考慮,iOS系統(tǒng)的沙盒機(jī)制規(guī)定每個(gè)應(yīng)用都只能訪問當(dāng)前沙盒目錄下面的文件(也有例外,比如系統(tǒng)通訊錄能...
    坤哥lqk閱讀 2,180評(píng)論 0 4
  • 前言: 出于安全考慮,iOS系統(tǒng)的沙盒機(jī)制規(guī)定每個(gè)應(yīng)用都只能訪問當(dāng)前沙盒目錄下面的文件(也有例外,比如系統(tǒng)通訊錄能...
    李sir35閱讀 16,559評(píng)論 4 19
  • //獲得應(yīng)用程序沙盒的Documents文件夾路徑** NSArray *arrDocumentPaths = N...
    博行天下閱讀 523評(píng)論 2 1
  • 昨天一天下午還真是個(gè)種查,各種搜索,然后各種技術(shù)群,各種問,(沒人鳥我),其實(shí)我是有這個(gè)能力的,怎么就一上...
    夢(mèng)隨興飛閱讀 25,317評(píng)論 7 18
  • 一、iOS中的沙盒機(jī)制 iOS應(yīng)用程序只能對(duì)自己創(chuàng)建的文件系統(tǒng)讀取文件,這個(gè)獨(dú)立、封閉、安全的空間,叫做沙盒。它一...
    1d5cb7cff98d閱讀 1,873評(píng)論 0 0

友情鏈接更多精彩內(nèi)容