2012-04-16 178 views
0

因此,我正在編寫的應用程序允許用戶從其相機膠捲上傳照片,並將其顯示在UIImageView中。我有一個「保存」按鈕,可以在按下時保存圖像。我也有一個「編輯」按鈕,點擊時,它允許用戶點擊照片,它將被刪除。這是我遇到的問題,圖像從視圖中刪除,但似乎沒有從數組中刪除。當我關閉並重新啓動應用程序時,照片再次出現。我對Objective-C非常陌生,我很驚訝我甚至做到了這一點,所以我在解決如何從數組中刪除它的問題。另外,作爲一個附註,如果我從相機膠捲中抓取3張圖像,刪除中間的一張,然後按下保存按鈕,關閉並重新啓動應用程序後,它只會顯示應該顯示的2張照片。關於重新啓動後圖像再次出現的問題僅在先按下保存,然後嘗試刪除它們時纔會出現。從NSMutableArray中刪除圖像

這就是我要如何刪除它們。我在每個UIImageView上都有一個不可見的按鈕,當按下Edit按鈕時,它將取消隱藏它們。我已經標記這兩個按鈕和UIImageViews的,並且當按下一個按鈕,這個火災:

- (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]; 
     } 

     ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil; 
    } 

    [self.user setObject:self.array forKey:@"images"]; 
} 

而且僅作參考,這是我如何保存圖像:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { 
     if (imageView.image == nil) { 
      imageView.image = img; 

      [self.array addObject:imageView]; 

      [picker dismissModalViewControllerAnimated:YES]; 
      [self.popover dismissPopoverAnimated:YES]; 

      return; 

     } 

     if (imageView2.image == nil) { 
      imageView2.image = img; 
      NSLog(@"The image is a %@", imageView); 
      [self.array addObject:imageView2]; 

      [picker dismissModalViewControllerAnimated:YES]; 
      [self.popover dismissPopoverAnimated:YES]; 

      return; 
     } 
     ... 
     ... 
     } 

    -(IBAction)saveButtonPressed:(id)sender { 
     NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0]; 



     for (UIImageView *imageViewToSave in self.array) { 

      NSInteger tag = imageViewToSave.tag; 
      UIImage *image = imageViewToSave.image; 
      NSString *imageName = [NSString stringWithFormat:@"Image%i.png",tag]; 

      NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName]; 

      [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:NO]; 
     } 

    } 

這是我如何加載它們:

-(void)viewDidLoad { 

     self.array = [NSMutableArray array]; 
     NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0]; 

     NSArray *docFiles = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:docsDir error:NULL]; 

     for (NSString *fileName in docFiles) { 

      if ([fileName hasSuffix:@".png"]) { 
       NSString *fullPath = [docsDir stringByAppendingPathComponent:fileName]; 
       UIImage *loadedImage = [UIImage imageWithContentsOfFile:fullPath]; 

       if (!imageView.image) { 
        imageView.image = loadedImage; 
       } 
       else if (!imageView2.image) { 
        imageView2.image = loadedImage; 
        } 
       else if (!imageView3.image) { 
        imageView3.image = loadedImage; 

       } 

      } 
     } 
    } 

更新:這裏是我當前的代碼:

- (IBAction)deleteButtonPressed:(id)sender { 
    NSLog(@"Sender is %@", 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]; 
      [fileManager removeItemAtPath:filePath error:NULL]; 
     } 

     ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil; 
    } 

    [self.user setObject:self.array forKey:@"images"]; 
} 

回答

2

當您從數組[self.array removeObject:view];刪除圖像時,您尚未從目錄中刪除圖像本身。

你可能想從array刪除對象後,做這樣的事情:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsPath = [paths objectAtIndex:0]; 
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"THE_IMAGE_NAME_HERE.png"]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
[fileManager removeItemAtPath:filePath error:&error]; 

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

感謝您的回答。所以,我應該添加[fileManager removeItemAtPath:查看錯誤:NULL];在[self.array removeObject:view];?下對於新手問題抱歉,對此仍然很陌生。 – John 2012-04-16 18:02:48

+1

我編輯了答案來顯示一個例子。 – sooper 2012-04-16 18:27:42

+0

非常感謝。對於'stringByAppendingPathComponent',我應該簡單地使用'view'嗎?我使用'view'來確定哪個UIImageView被刪除的標籤。 – John 2012-04-17 01:27:06

2

您的數組與此無關。無論如何,它會在下一次啓動時被覆蓋。我不認爲你曾經從文件夾中刪除你的圖像,所以它會在你的viewDidLoad方法的下一次啓動時再次讀取。

+0

啊是啊,這是有道理的,謝謝。我認爲'[self.array removeObject:view];'和'((UIImageView *)[self.view viewWithTag:alertView.tag])。image = nil;'會做的伎倆。你知道我需要添加什麼來從文件夾和數組中刪除圖像嗎?對於新手的問題再次抱歉,當我去這裏時,我仍然在學習。 – John 2012-04-16 03:41:10

+0

圖像不是文件參考,它是指向存儲圖像的內存位置的指針。您必須使用nsfilemanager顯式刪除文件。 – borrrden 2012-04-16 04:42:10

+0

謝謝。所以,我應該添加'[fileManager removeItemAtPath:view error:NULL];'''[self.array removeObject:view];'? – John 2012-04-16 16:00:44