iphone document 圖片存儲和讀取

存: 
Java代碼  收藏代碼
  1. //此處首先指定了圖片存取路徑(默認寫到應用程序沙盒 中)  
  2.   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
  3.     
  4.   //並給文件起個文件名  
  5.   NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];  
  6.   BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];  
  7.   if (blHave) {  
  8.       NSLog(@"already have");  
  9.       return ;  
  10.   }  
  11.   //此處的方法是將圖片寫到Documents文件中 如果寫入成功會彈出一個警告框,提示圖片保存成功  
  12.   NSString *strPathOld = [[NSBundle mainBundle] pathForResource:@"pin" ofType:@"png"];  
  13.   NSData *data = [NSData dataWithContentsOfFile:strPathOld];  
  14.   BOOL result = [data writeToFile:uniquePath atomically:YES];  
  15.   if (result) {  
  16.       NSLog(@"success");  
  17.   }else {  
  18.       NSLog(@"no success");  
  19.   }  


取: 
Java代碼  收藏代碼
  1. NSFileManager *fileManager = [NSFileManager defaultManager];  
  2.   NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,                                                                          NSUserDomainMask, YES);  
  3.   NSString *documentsDirectory = [paths objectAtIndex:0];  
  4.   NSString *filePath2 = [documentsDirectory stringByAppendingPathComponent:@"pin.png"];  
  5.   UIImage *img = [UIImage imageWithContentsOfFile:filePath2];  
  6.   [image setImage:img];  
  7.   NSLog(@"圖片:::::::::%@",image.image);  


刪: 

Java代碼  收藏代碼
  1. NSFileManager* fileManager=[NSFileManager defaultManager];  
  2.    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
  3.      
  4.    //文件名  
  5.    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"pin.png"];  
  6.    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];  
  7.    if (!blHave) {  
  8.        NSLog(@"no  have");  
  9.        return ;  
  10.    }else {  
  11.        NSLog(@" have");  
  12.        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];  
  13.        if (blDele) {  
  14.            NSLog(@"dele success");  
  15.        }else {  
  16.            NSLog(@"dele fail");  
  17.        }  
  18.          
  19.    }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章