2012-04-05 64 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:,它具有使用系统缓存的附加奖励,并且还会根据设备的特性自动加载图像的正确版本(例如,它会加载更高分辨率的变体如果设备具有视网膜显示,则为图像)。