I have an application that has an area that is supposed to behave somewhat like a photo gallery. The user chooses a picture from their camera roll, and the photos get displayed in UIImageViews. I’m trying to save the image that they select. I have 9 UIImageView’s, and the issue is that when I select a different photo for each UIImageView, close and relaunch the app, all of the UIImageViews are blank. Here is the code that I’m working with:
- (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;
}
Everything looks okay to me, so I can’t figure out why they are not saving / loading. Any help is much appreciated, thanks!
You assign
self.userinviewWillAppearwhich is called afterviewDidLoad.You should change
viewDidLoadto: