2010-03-30 57 views
0

我正在與其他人開發iPhone應用程序。該應用程序適用於我,但他遇到了一個bug。我們認爲這個bug與他爲同一個應用程序獲取多個Application目錄有關。在我的〜/ Library/Application Support/iPhone Simulator/User/Applications中,我一直只有一個文件夾。適用於iPhone模擬器的多個應用程序支持目錄?

他說當他只在這個應用上工作時,他會得到3或4個目錄。我們認爲這是我們的問題,因爲我們的錯誤與顯示存儲在應用程序的「文檔」文件夾中的圖像有關。有誰知道他爲什麼要結束多個目錄或如何阻止它?

編輯:

這裏是圖像寫入文件的代碼:

NSData *image = [NSData dataWithContentsOfURL:[NSURL URLWithString:[currentArticle articleImage]]]; 

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

      NSFileManager *NSFM = [NSFileManager defaultManager]; 
      BOOL isDir = YES; 

      if(![NSFM fileExistsAtPath:imagePath isDirectory:&isDir]) 
       if(![NSFM createDirectoryAtPath:imagePath attributes:nil]) 
        NSLog(@"error"); 

      imagePath = [imagePath stringByAppendingFormat:@"/images"]; 

      if(![NSFM fileExistsAtPath:imagePath isDirectory:&isDir]) 
       if(![NSFM createDirectoryAtPath:imagePath attributes:nil]) 
        NSLog(@"error"); 

      imagePath = [imagePath stringByAppendingFormat:@"/%@.jpg", [currentArticle uniqueID]]; 

      [image writeToFile:imagePath atomically:NO]; 

這裏是獲取路徑的代碼時,我需要的圖像:

- (NSString *)imagePath 
{ 
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *imagePath = [array objectAtIndex:0]; 
    return [imagePath stringByAppendingFormat:@"/images/%@.jpg", [self uniqueID]]; 
} 

該應用程序對我很好,但我的合作伙伴說圖像不會間歇性顯示,並且他注意到他在他的Appl中獲取了多個目錄文件夾。

回答

0

我有這個問題(我將照片保存在應用程序文檔目錄中),並且在每次新建立後,目錄get被重命名,所以我的路徑不再有效。我製作了這兩個函數(在我的應用程序委託中),它會爲我想要保存或從文檔或臨時目錄加載的文件提供路徑。即使應用程序目錄發生變化,只要您只存儲文件名而不是完整路徑,然後使用助手函數在稍後需要時獲取路徑,您將可以。這是我的功能:

+ (NSString*)fullPathToFile:(NSString*)file { 
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0]; 

return [documentsDirectory stringByAppendingPathComponent:file]; 
} 

+ (NSString*)fullPathToTemporaryFile:(NSString*)file { 
return [NSTemporaryDirectory() stringByAppendingPathComponent:file]; 
} 

工程就像一個魅力。

+0

嗯..我們已經在使用這段代碼了。也許我們的問題在於別處。 – 2010-03-30 21:53:47

+0

也許吧。也許如果你發佈了一些代碼,我們可能會發現問題。 – 2010-03-30 22:07:58

+0

我添加了代碼 - 注意它有什麼不對嗎? – 2010-03-30 22:59:02

相關問題