2012-02-23 77 views
6

我想從我的iPhone應用程序中刪除圖像。 我使用下面的方法,將圖像的名稱作爲參數傳遞。從iPhone中的應用程序目錄中刪除圖像

問題是圖像未被刪除。

- (void)removeImage:(NSString*)fileName { 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent: 
         [NSString stringWithFormat:@"%@.png", fileName]]; 

    [fileManager removeItemAtPath: fullPath error:NULL]; 
    NSLog(@"image removed: %@", fullPath); 

    NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];  
    NSLog(@"Directory Contents:\n%@", [fileManager directoryContentsAtPath: appFolderPath]); 
} 

最後兩行顯示我的應用程序目錄中的內容,我想要刪除的圖像仍然存在。我究竟做錯了什麼?

回答

5

您正試圖刪除Documents目錄中的文件。然後閱讀捆綁資源目錄的內容。這些不是同一個目錄。

如果您要刪除Documents目錄中的文件,則應在最後使用NSLog()中的該目錄。如果你試圖刪除你的包內的文件,這是不可能的。應用程序包已簽名且無法修改。

+0

我試圖刪除我在XCode中添加的圖像,所以它可能在我的包中。這是默認圖像。我試圖刪除這個並添加一個新的。 – bruno 2012-02-23 14:59:13

+1

您無法刪除AppBundle中的圖像(這些圖像是您在Xcode中添加的圖像)。 – rckoenes 2012-02-23 15:04:42

+0

我不能替換默認圖像,而不是刪除它嗎? – bruno 2012-02-23 15:18:00

4

你的代碼看起來不錯,所以嘗試加入一些「NSError」對象,你的代碼:

- (void)removeImage:(NSString*)fileName { 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent: 
         [NSString stringWithFormat:@"%@.png", fileName]]; 

    NSError *error = nil; 
    if(![fileManager removeItemAtPath: fullPath error:&error]) { 
     NSLog(@"Delete failed:%@", error); 
    } else { 
     NSLog(@"image removed: %@", fullPath); 
    } 

    NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];  
    NSLog(@"Directory Contents:\n%@", [fileManager directoryContentsAtPath: appFolderPath]); 
} 

在上面的代碼我通過NSError removeItemAtPath的誤差參數。如果系統無法刪除該文件,則此方法將返回NO並填充發生錯誤的error對象。

+0

「操作無法完成。沒有這樣的文件或目錄」我如何搜索文件或文件夾?因爲它似乎有另一個文件夾的圖像保存英寸 – bruno 2012-02-23 14:51:39

+0

那麼你在哪裏保存圖像的第一個地方。 – rckoenes 2012-02-23 15:03:56

2

根據你的評論,我發現你試圖刪除default.png並將其替換爲另一個。不幸的是,這是不可能的。 default.png圖像是應用程序包的一部分,創建並簽名後不能再進行修改(這是來自Apple的安全措施,因此應用程序在經過審覈後無法更改)。您可以創建和刪除文件的唯一位置位於爲應用程序提供的沙箱內(Documents文件夾)。

+0

這是在蘋果的指導方針?因爲我找不到與此相關的任何內容。 – bruno 2012-02-23 15:16:20

+0

它曾經是在「iOS應用程序編程指南」(這裏引用的部分:http://stackoverflow.com/questions/8122267/app-bundle-protection-signed-bundle-may-be-修改),但他們似乎修改它。我環顧四周,發現:https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/AppSigning.html這似乎表明只有可執行部分實際簽名。然而afaik,他們沒有提供一種在運行時真正進行修改的方法。 – 2012-02-24 09:55:48

+0

下面是關於他們打算讓你能夠做多少的部分:https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/App-RelatedResources/App-RelatedResources.html# // apple_ref/DOC/UID/TP40007072-CH6-SW12 – 2012-02-24 09:57:52

相關問題