我对Objective-C并不陌生,并且遇到了一些初学者问题。我有一个应用程序,该应用程序的行为应该像照相馆一样。用户从其相机胶卷中选择一张图片,然后这些图片将显示在UIImageViews中。我正在尝试保存他们选择的图像。我有9个UIImageView,问题是当我为每个UIImageView选择不同的照片时,关闭并重新启动该应用程序,其他8个UIImageViews将显示存储在第一个图像视图中的照片。这是我正在使用的代码:

- (NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                                         NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFilename9];
}

- (void)applicationDidEnterBackground:(UIApplication*)application {
    NSLog(@"Image on didenterbackground: %@", imageView);
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView.image)];
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView2.image)];
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView3.image)];
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView4.image)];
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView5.image)];
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView6.image)];
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView7.image)];
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView8.image)];
    self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView9.image)];

    [self.imageData writeToFile:[self dataFilePath] atomically:YES];
    NSLog(@"The image is: %@", [[imageView image] description]);
    NSLog(@"dataFilePath is: %@", [self dataFilePath]);

}

- (void)viewDidLoad
{

    NSString *filePath = [self dataFilePath];
    NSLog(@"FilePath: %@", filePath);
    NSLog(@"Image: %@", imageView);
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSData *vdlData = [[NSData alloc] initWithContentsOfFile:filePath];
        imageView.image = [[UIImage alloc] initWithData:vdlData];
        imageView2.image = [[UIImage alloc] initWithData:vdlData];
        imageView3.image = [[UIImage alloc] initWithData:vdlData];
        imageView4.image = [[UIImage alloc] initWithData:vdlData];
        imageView5.image = [[UIImage alloc] initWithData:vdlData];
        imageView6.image = [[UIImage alloc] initWithData:vdlData];
        imageView7.image = [[UIImage alloc] initWithData:vdlData];
        imageView8.image = [[UIImage alloc] initWithData:vdlData];
        imageView9.image = [[UIImage alloc] initWithData:vdlData];
    }


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

    [super viewDidLoad];

}


我试图弄清楚我需要更改什么以使UIImageViews显示正确的图片,而不是全部显示相同的图片。这可能是一个简单的修复程序,但是任何帮助将不胜感激,谢谢!

最佳答案

好的,这是我的做法:

使用NSUserDefaults将图像保存为可变数组:

ViewController.h

@property(retain) NSUserDefaults *user;


ViewController.m

@synthesize user;

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


编辑

    if(array.count == 9)
    {
    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;
}


这样,您将不会丢失图像或数据,它们将被存储并易于访问,并且即使您更新应用程序也不会消失。

干杯!

10-07 19:54
查看更多