2012-04-24 58 views
1

所以我差不多完成了一個本應該像照片庫的應用程序。儘管試圖正確刪除照片,但我遇到了一個新手問題。所以,我有3個UIImageViews。圖像存儲在NSMutableArray中,並在啓動時從中加載。它們完全刪除並保存。但是,如果我要在UIImageView#2中刪除圖像,然後關閉並重新啓動應用程序,則來自UIImageView#3的圖像會滑入#2的位置。這很好,一切,直到我嘗試刪除#2的位置或#2以上的任何圖像。刪除合適的圖像

我刪除了基於名稱的照片,所以如果我嘗試刪除UIImageView#2中的照片,我的代碼將搜索Image2.png並將其刪除。但是,現在當我重新啓動應用程序時,UIImageView#3位於#2的位置,所以當我嘗試刪除UIImageView#3時,它將不會刪除,因爲它尋找Image2.png,它不再存在。該文件的名稱是Image3.png。所以,我有些茫然,不知道如何編輯我的代碼來解決這個問題。這裏是我正在使用的當前代碼,這裏的罪魁禍首是NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%i.png",view.tag]];。 (我所標記的每的UIImageView和我依據,刪除):

- (IBAction)deleteButtonPressed:(id)sender { 
    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete" 
                   message:@"Are you sure you want to delete this photo?" 
                  delegate:self 
                cancelButtonTitle:@"No" 
                otherButtonTitles:@"Yes", nil]; 
    [deleteAlertView show]; 

    int imageIndex = ((UIButton *)sender).tag; 
    deleteAlertView.tag = imageIndex; 
} 

- (UIImageView *)viewForTag:(NSInteger)tag { 
    UIImageView *found = nil; 
    for (UIImageView *view in self.array) { 
     if (tag == view.tag) { 
      found = view; 
      break; 
     } 
    } 
    return found; 
} 

- (void)alertView: (UIAlertView *) alertView 
clickedButtonAtIndex: (NSInteger) buttonIndex 
{ 


    if (buttonIndex != [alertView cancelButtonIndex]) { 

     UIImageView *view = [self viewForTag:alertView.tag]; 
     if (view) { 

      [self.array removeObject:view]; 

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsPath = [paths objectAtIndex:0]; 
      NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%i.png",view.tag]]; 
      NSFileManager *fileManager = [NSFileManager defaultManager]; 

      NSError *error; 

      [fileManager removeItemAtPath:filePath error:&error]; 

      if (error) 
       NSLog(@"Error: %@",error); 

     } 
     ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil; 
    } 
    [self.user setObject:self.array forKey:@"images"]; 
} 

這裏是我的全部.m文件僅供參考: https://github.com/Joey502/Test-Thing/wiki/.m-Class-File

如果有人知道如何解決這個問題,那會太好了!

回答

3

你想要做什麼是創建另一個NSMutableArray,稱之爲fileNamesArray存儲相關的每一個文件名(以相同的順序)存儲在您的self.arrayUIImageView秒。

當啓動應用程序,你應該在每個self.fileNamesArray文件名(NSString S)存儲在同一時間您添加UIImageView s到self.array。然後,在刪除功能中,當您從self.array中刪除UIImageView對象並從目錄中刪除它時,還應從self.fileNamesArray中刪除文件名。因此,一旦從兩個陣列中刪除了對象,它們將全部移位相同的數量,並且每個文件名將對應於正確的圖像視圖。

我想補充到:當刪除圖像時,你應該使用sendertag從您的self.fileNamesArray檢索的文件名,你會再使用刪除物理文件,隨後你會從數組中刪除它(以及圖像視圖)。這裏是你的刪除功能應該是什麼樣子的例子:

if (view) { 
    [self.array removeObject:view]; //or you could do [self.array removeObjectAtIndex:alertView.tag]; 

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

    NSString *filePath = [documentsPath stringByAppendingPathComponent:[self.fileNamesArray objectAtIndex:alertView.tag]]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSError *error; 

    [fileManager removeItemAtPath:filePath error:&error]; 

    if (error) 
     NSLog(@"Error: %@",error); 

    [self.fileNamesArray removeObjectAtIndex: alertView.tag]; 
} 

更新,以解決意見中提出的問題:

  1. 在你applicationDidEnterBackground方法,您要添加的所有圖像視圖爲self.array再次。如果數組不是空的呢?然後,您只需添加不需要的項目。這種方法中唯一需要的是最後兩行。

  2. 在你viewDidLoad方法,你應該被添加fileNameself.fileNamesArray,但你已經添加了imageView。刪除所有圖像視圖添加到該陣列的線條和UIImage *loadedImage = [UIImage imageWithContentsOfFile:fullPath];後添加此行[self.fileNamesArray addObject:fileName];

  3. 在你刪除功能,此行缺少兩個支架:NSString *filePath = [documentsPath stringByAppendingPathComponent:[self.fileNamesArray objectAtIndex:alertView.tag;。它應該是NSString *filePath = [documentsPath stringByAppendingPathComponent:[self.fileNamesArray objectAtIndex:alertView.tag]];。我想知道這是爲什麼你的整個目錄被刪除。

  4. 不要忘記,當您使用圖像選擇器添加圖像時,還應該在選取器代理方法中(按正確的順序)將文件名添加到fileNamesArray

+0

非常感謝,我嘗試了所有上述步驟,但最終刪除我的整個Documents文件夾,而不僅僅是畫面,笑從未見過這種情況發生。 – John 2012-04-26 16:02:49

+0

你知道爲什麼會發生這種情況嗎? – John 2012-04-30 02:24:37

+0

也許放置'的NSLog(@ 「刪除文件%@」,[self.fileNamesArray objectAtIndex:alertView.tag]);'刪除之前。這可能是您的數組尚未正確設置/更新。 – sooper 2012-04-30 11:52:50

1

檢查您使用self.array。有時候,您正在使用它來存儲/檢索UIImageView的其他地方,您正在存儲/檢索圖像視圖中的圖像。

如果你要使用self.array兩種不同的目的,那麼你要麼需要釋放和交換的目的時,分配一個新的,或者切換到其他用途之前,刪除所有的當前內容。