iOS:问题保存照片

时间:2012-03-30 21:52:05

标签: objective-c ios uiimageview save

我对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显示正确的图片,而不是全部显示相同的图片。这可能是一个简单的修复,但是非常感谢任何帮助,谢谢!

3 个答案:

答案 0 :(得分:2)

在我开始使用解决方案之前,我必须警告你,你这样做的方式并不正确。我建议你从头开始学习iOS开发。 Apple自己的文档是一个非常好的开始。 http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html

现在,回到你的问题。你在这里做的只是保存一个图像,而不是全部9个图像。您始终为处理的每个图像设置self.imageData,并覆盖其先前的值,只将最后一个图像保存到文件中。

因此,为了使您的代码正常工作,您必须为每个图像视图使用imageData对象,然后将该数据对象写入文件。

在您的情况下,最好通过使用循环来优化代码,而不是使用多个对象(例如imageViewimageView2,...)。

另外,请确保照顾好自己的记忆。例如imageView.image已分配但尚未发布。

答案 1 :(得分:2)

好的,我会这样做:

使用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;
}

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

干杯!

答案 2 :(得分:1)

好吧,我看到两个问题。首先,在viewDidLoad中,所有图像都获得initWithData:vdlData ...所以它们都获得了相同的数据。这就是为什么他们都是一样的。

此外,当您尝试保存它们时,在...didEnterBackground中,您一遍又一遍地覆盖imageData的值...当您保存它时,它只是最后一个你已分配给imageData。您可能想要创建一个NSArray,并将它们存储在那里,将它们从viewDidLoad中的数组中拉出来。