iOS:NSUserDefaults和UIImageViews

时间:2012-04-01 00:09:45

标签: objective-c ios uiimageview nsuserdefaults

我有一个应用程序,其区域应该有点像照片库。用户从相机胶卷中选择一张照片,照片将显示在UIImageViews中。我正在尝试保存他们选择的图像。我有9个UIImageView,问题是当我为每个UIImageView选择不同的照片,关闭并重新启动应用程序时,所有的UIImageView都是空白的。这是我正在使用的代码:

- (void)viewWillAppear:(BOOL)animated
{

    self.user = [NSUserDefaults standardUserDefaults];

    NSMutableArray* array = [[self.user objectForKey:@"images"]mutableCopy];
    while(array == nil)
    {
        [self.user setObject:[NSMutableArray arrayWithObject:@""] forKey:@"images"];
        array = [[self.user objectForKey:@"images"]mutableCopy];
        NSLog(@"%@",@"attempting to create an array to store the images in");
    }

}

- (void)applicationDidEnterBackground:(UIApplication*)application {
    NSLog(@"Image on didenterbackground: %@", imageView);
    NSMutableArray* array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]];

    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]];
     [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]];
      [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView4.image)]];
       [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView5.image)]];
        [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView6.image)]];
         [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView7.image)]];
          [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView8.image)]];
           [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView9.image)]];

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

            }

- (void)viewDidLoad
    {
        NSMutableArray* array = [[self.user objectForKey:@"images"]mutableCopy];
        imageView.image = [[UIImage alloc] initWithData:[array objectAtIndex:0]];
        imageView2.image = [[UIImage alloc] initWithData:[array objectAtIndex:1]];
        imageView3.image = [[UIImage alloc] initWithData:[array objectAtIndex:2]];
        imageView4.image = [[UIImage alloc] initWithData:[array objectAtIndex:3]];
        imageView5.image = [[UIImage alloc] initWithData:[array objectAtIndex:4]];
        imageView6.image = [[UIImage alloc] initWithData:[array objectAtIndex:5]];
        imageView7.image = [[UIImage alloc] initWithData:[array objectAtIndex:6]];
        imageView8.image = [[UIImage alloc] initWithData:[array objectAtIndex:7]];
        imageView9.image = [[UIImage alloc] initWithData:[array objectAtIndex:8]];



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


        [super viewDidLoad];

    }

- (void)viewDidUnload
    {
        self.user = nil;
    }

一切看起来都不错,所以我无法弄清楚他们为什么不保存/加载。非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:2)

您在self.user之后调用的viewWillAppear中分配viewDidLoad

您应该将viewDidLoad更改为:

-(void)viewDidLoad {
  self.user = [NSUserDefaults standardUserDefaults];
  // ... rest the same

答案 1 :(得分:1)

我不喜欢你的方法(使用NSUserDefaults来存储潜在的大数据)。也就是说,尝试在synchronize上调用NSUserDefaults,在完成applicationDidEnterBackground之前将它们写入持久存储。当您的应用程序运行时,通常会定期调用该方法,但是当它进入后台时,它可能不会自动调用。