2013-02-27 50 views
1

我有一個應用程序,其中有一定數量的.jpg圖片(大約爲300)。因爲它們實際上位於互聯網中,所以它們可以作爲開始的東西,但顯然它更方便用戶在應用程序首次啓動時不要下載所有應用程序,而是預先打包它們。將應用程序中的圖像複製到文檔目錄並在它之後重寫它們

我需要每次從服務器獲取新信息時重寫這些圖像。顯然,我無法觸摸應用程序包,因此我看到我的步驟如下:

  1. 將應用程序首次啓動時從捆綁包中的圖像解壓縮到Documents Directory。
  2. 僅從文檔目錄訪問它們,但不從捆綁軟件訪問它們。
  3. 如果有必要,我應該重寫它們。

因此,我的代碼將統一,因爲我將始終使用相同的路徑來獲取圖像。

問題是,我對iOS中的整個文件系統事情知之甚少,所以我不知道如何將特定的包內容解壓縮到Documents Directory,並且我也不知道如何寫入Documents Directory無論是。

你能幫我一些代碼,並確認我的解決方案是正確的嗎?

回答

2
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"images"]; //optionally create a subdirectory 

//"source" is a physical folder in your app bundle. Once that has a blue color folder (not the yellow group folder) 
// To create a physical folder in your app bundle: drag a folder from Mac's Finder to the Xcode project, when prompts 
// for "Choose options for adding these files" make certain that "Create folder references for …" is selected. 
// Store all your 300 or so images into this physical folder. 

NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"source"]; 
NSError *error; 
[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destPath error:&error]; 
if (error) 
    NSLog(@"copying error: %@", error); 

從OP每增加評論編輯:

要使用相同的文件名相同的目錄重寫,您可以使用fileExistsAtPath和removeItemAtPath的組合來檢測和寫入之前刪除現有文件。

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
{ 
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]; 
} 
// now proceed to write-rewrite 
+0

感謝,但對於在該目錄中重寫這些文件? – morgan1189 2013-02-27 19:32:50

+1

@ morgan1189只是重寫它們就像你寫一個通常的文件到磁盤 – 2013-02-27 21:19:26

+0

它不會給我任何錯誤嗎? – morgan1189 2013-02-27 21:27:50

0

試試這個代碼

-(void)demoImages 
{ 

//-- Main bundle directory 
NSString *mainBundle = [[NSBundle mainBundle] resourcePath]; 
NSFileManager *fm = [NSFileManager defaultManager]; 
NSError *error = [[NSError alloc] init]; 
NSArray *mainBundleDirectory = [fm contentsOfDirectoryAtPath:mainBundle error:&error]; 

NSMutableArray *images = [[NSMutableArray alloc]init]; 
for (NSString *pngFiles in mainBundleDirectory) 
{ 
    if ([pngFiles hasSuffix:@".png"]) 
    { 
     [images addObject:pngFiles]; 
    } 
} 

NSLog(@"\n\n Doc images %@",images); 
//-- Document directory 
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentDirectory = [paths objectAtIndex:0]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

//-- Copy files form main bundle to document directory 
for (int i=0; i<[images count]; i++) 
{ 
    NSString *toPath = [NSString stringWithFormat:@"%@/%@",documentDirectory,[images objectAtIndex:i]]; 
    [fileManager copyItemAtPath:[NSString stringWithFormat:@"%@/%@",mainBundle,[images objectAtIndex:i]] toPath:toPath error:NULL]; 
    NSLog(@"\n Saved %@",fileManager); 
} 


} 
相關問題