2014-02-26 28 views
1
  • 我正在項目中,我需要自定義圖像庫,其中 圖像來自只有特定的目錄。
  • 所以,我在創建文檔目錄一個目錄,並將從包路徑目錄下的文件名爲「ImageFolder」的名單 如圖 截圖。獲取目錄BundlePath中的內容列表

  • 我試過不同的代碼,但總是得到數組nil.I知道有沒有文件夾在我得到的路徑,但如何做請告訴我。

- 我想要什麼>

  1. 創建文檔目錄目錄 「XYZ」。

  2. 將「ImageFolder」中的所有文件複製到「xyz」中。

enter image description here

// ==================================== ============================

enter image description here

// =========== ================================================== ===

enter image description here

+0

您可以將目錄複製到imagefolder目錄中以查找目錄。你可以使用這個鏈接http://stackoverflow.com/questions/17669944/copy-folder-from-main-bundle-to-documents-directory-in-iphone –

回答

1

您顯示的ImageFolder的屏幕截圖是Xcode項目中的一個組,並不意味着與該組相關的任何內容都將被複制到應用程序中。如果該應用程序中的圖像處於您的複製束資源構建階段,那麼它們將直接複製到應用程序束(而不是子文件夾)中。

您需要添加一個新的複製文件構建階段,設置文件夾名稱並將圖像移動到該文件夾​​中。

一旦你這樣做,考慮你爲什麼要複製圖像。用戶可以進行一些編輯嗎?如果沒有,那麼複製圖像是浪費空間。如果用戶可以「刪除」某些圖像,那麼考慮將「刪除項目」plist或用戶默認值存儲爲隱藏/過濾圖像列表。

+0

雅,謝謝我以同樣的方式思考,使plist需要images.And使用該plist添加圖像到自定義庫。 –

1

此代碼的工作

-(void) copyDirectory:(NSString *)directory { 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory]; 
    NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory]; 

    if (![fileManager fileExistsAtPath:documentDBFolderPath]) { 

     //Create Directory! 
     [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:&error]; 

     NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourceDBFolderPath error:&error]; 
     for (NSString *s in fileList) { 
      NSString *newFilePath = [documentDBFolderPath stringByAppendingPathComponent:s]; 
      NSString *oldFilePath = [resourceDBFolderPath stringByAppendingPathComponent:s]; 
      if (![fileManager fileExistsAtPath:newFilePath]) { 
       //File does not exist, copy it 
       [fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error]; 
      } else { 
       [fileManager removeItemAtPath:newFilePath error:&error]; 
       [fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error]; 
      } 
     } 

     [self info:@"Copy finish!"]; 
    } 
} 

在目錄中添加文件夾的名稱複製

編輯:

您的文件夾必須通過引用加入。您的文件夾拖放到你的項目在Xcode並選擇「創建文件夾引用任何添加的文件夾」

enter image description here

+0

nope我已經使用這個不工作 –

+0

你需要通過引用添加你的文件夾,我更新我的答案。您的文件夾必須是藍色沒有黃色 –

+0

雅關於此我不知道它現在的工作.....謝謝 –

0

你有什麼是目前的一組(黃色文件夾圖標)(身體,他們不是在一個子文件夾) - 你需要做的是在Xcode之外創建一個文件夾,放入你需要的圖像,然後將它拖入你的項目 - 你應該有一個藍色的文件夾圖標,你的命令應該工作。

另一件事,只要引用它,這樣你就不需要繼續向Xcode介紹你的圖像,每次它只是將它撿起來,然後她將它編譯並放入包中。

+0

是的,我已經這樣做了..謝謝。 –