2012-04-05 33 views
0

我將圖像存儲在NSMutableArray中,現在我試圖讓它們顯示在viewDidLoad中。我試着調用initWithContentsOfFile,但這似乎不起作用。這是怎樣的代碼看起來: imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]];-initWithContentsOfFile:對於NSMutableArray

我不知道我應該使用initWithContentsOfFile的有保存的圖像加載,我甚至不知道我是否能夠通過用戶默認保存在plist中的圖像。我一直在研究它一段時間無濟於事。任何幫助非常感謝,謝謝!

編輯:這裏是額外的代碼:

- (IBAction)grabImage { 
    self.imgPicker = [[UIImagePickerController alloc] init]; 
    self.imgPicker.delegate = self; 
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
     _popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker]; 
     [_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    } 

    else { 
     [self presentModalViewController:imgPicker animated:YES]; 
    } 
    [self.imgPicker resignFirstResponder]; 
} 
// Sets the image in the UIImageView 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { 
    if (imageView.image == nil) { 
     imageView.image = img; 

     [self.array addObject:imageView.image]; 

     [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.image]; 

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

    if (imageView3.image == nil) { 
     imageView3.image = img; 

     [self.array addObject:imageView3.image]; 

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

- (void)applicationDidEnterBackground:(UIApplication*)application { 
    NSLog(@"Image on didenterbackground: %@", imageView); 

    [self.array addObject:imageView.image]; 
    [self.array addObject:imageView2.image]; 
    [self.array addObject:imageView3.image]; 


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

      } 

- (void)viewDidLoad 
    { 
     self.user = [NSUserDefaults standardUserDefaults]; 
     NSLog(@"It is %@", self.user); 
     self.array = [[self.user objectForKey:@"images"]mutableCopy]; 
     imageView.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:0]]; 
     imageView2.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:1]]; 
     imageView3.image = [[UIImage alloc] initWithContentsOfFile:[self.array objectAtIndex:2]]; 




     UIApplication *app = [UIApplication sharedApplication]; 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(applicationDidEnterBackground:) 
                name:UIApplicationDidEnterBackgroundNotification 
                object:app]; 

     backToGalleryButton.hidden = YES; 
     tapToDeleteLabel.hidden = YES; 
     deleteButton1.hidden = YES; 
     [super viewDidLoad]; 

    } 

編輯:這是我如何標記的圖像,並刪除它們根據自己的標籤:

- (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]) { 
     NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]); 
     NSLog(@"The tag is %i", alertView.tag); 

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

     NSLog(@"After deleting item, array count = %d", [array count]); 
    NSLog(@"Returned view is :%@, in view: %@", [self.view viewWithTag:alertView.tag], self.view); 
     ((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil; 
    } 

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

你有崩潰嗎?報告的錯誤是什麼? – joerick 2012-04-05 23:43:59

+0

@joerick不,先生,我不會崩潰。但是,當我關閉應用程序時(也在多任務欄中),圖像不能正確保存/加載並重新啓動應用程序。我有一種感覺,initWithContentsOfFile是罪魁禍首。 – John 2012-04-05 23:47:04

+0

實際存儲在數組中的內容是什麼?一條路徑?一個URL?如果它只是存儲一個UIImage實例,則不能使用initWithContentsOfFile ... – Daniel 2012-04-05 23:49:52

回答

2

問題是,您無法將圖像存儲在屬性列表中,這是您在將其保存爲用戶默認值時要執行的操作。您需要使用存檔器將圖像轉換爲可存儲的NSData對象。

+0

我之前就是這麼做的,但我需要標記圖像。 NSData沒有標籤屬性,我可以檢查。 – John 2012-04-06 00:39:28

+0

你是如何標記圖像的?我沒有看到代碼中有關於此的任何內容。 – rdelmar 2012-04-06 00:48:30

+0

我在界面生成器中標記它們(以允許用戶刪除圖像,如果他們想)。我只是用我用來做這些的代碼更新了OP。 – John 2012-04-06 00:52:02

0

它看起來像你不會將有效的圖像路徑傳遞給初始化方法。確保路徑正確,並且包含圖像擴展名。

真的,在這種情況下,您不應該撥打initWithContentsOfFile:,因爲UIImageViewimage屬性在您設置時會保留圖像。這通常會導致內存泄漏(除非您使用自動引用計數)。改爲使用其中一種靜態初始化器,如imageNamed:,它具有使用系統緩存的附加獎勵,並且還會根據設備的特性自動加載圖像的正確版本(例如,它會加載更高分辨率的變體如果設備具有視網膜顯示,則爲圖像)。