2012-03-30 16 views
0

私はObjective-Cの新機能で、初心者の問題を抱えています。私は写真ギャラリーのように振る舞うはずの領域があるアプリケーションを持っています。ユーザーはカメラロールから写真を選択すると、写真がUIImageViewsに表示されます。彼らが選択した画像を保存しようとしています。私は9 UIImageView'sを持っています。問題は、UIImageViewごとに別の写真を選択して閉じると再起動すると、他の8つのUIImageViewsが最初の画像ビューに保存されている写真を表示するということです。私はむしろそれらすべてより同じ画像を表示し、私は正しい画像を表示するためにUIImageViewsを取得するために変更する必要があるかを把握しようとしているiOS:写真を保存する問題

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

} 

:ここでは私が働いているコードがあります。これはおそらく簡単な修正ですが、どんな助けでも大歓迎です。ありがとう!

+2

vdlData ...に保存された同じイメージを持つ各imageViewを割り当てていることに気付きましたか?そして、1つのパス(dataFilePath)に保存する前に、各イメージを同じ変数に保存しています – David

+0

ああ、どうして私はそれを見逃しましたか?ありがとうございました。すべての変数を1つのパスに保存しても問題ありませんか、各変数ごとに異なるパスを設定する必要はありますか? – John

+1

ここで私はそれを答えにします。 – David

答えて

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

EDIT

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

のEND EDIT

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

    [super viewDidLoad]; 

} 

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

このように、あなたは、彼らは、アクセスしやすい保存され、画像やデータを失うことはありません、あなたのアプリを更新しても、彼らは消えません。

乾杯!

+0

David、あなたのお手伝いをしてくれてありがとう! – John

+1

問題ありません。先日、あなたの質問をもう一度お手伝いしました。ハハ、小規模なサイトですが、そうではありません。 – David

+0

あなたは人をやったよ、あなたは命を救ってきた。もう1つ質問がありますが、私はあなたのコードを実装していますが、何らかの理由で画像を保存できない場合は、どこにいても返すべきですか?再びタクス! – John

2

私が解決策を開始する前に、あなたがこれをやっているやり方が正しいとは言えません。私はあなたがiOSの開発を最初から学び始めることをお勧めします。 Apple独自のドキュメントはかなり良いスタートです。 http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html

あなたの質問に戻ってください。あなたがここで行うことは、1つの画像だけを保存することであり、9つすべてを保存することではありません。処理する各画像に常にself.imageDataを設定し、前の値を上書きして、最後の画像のみをfileに保存します。

コードを機能させるには、画像ビューごとにimageDataオブジェクトを使用し、そのデータオブジェクトをファイルに書き込む必要があります。

複数のオブジェクト(imageViewimageView2、...など)を使用する代わりに、ループを使用してコードを最適化するのが最善の方法です。

また、メモリを十分に配慮してください。例えばimageView.imageは割り当てられていますが、リリースされていません。

+1

-1最初の段落を具体的なアドバイスに変えてください。削除してください。また、ミニチュアコードブロックを読みやすくするためにバックティックを使用しても構いません。私は答えを修正するときにマイナス1を逆にすることができます。 –

+2

今、それは大丈夫でしょう。 –

1

まあ、2つの問題があります。まず第一に、viewDidLoadでは、すべての画像がinitWithData:vdlData ...を取得しているので、すべて同じデータを取得しています。そういうわけで、彼らはすべて同じです。

...didEnterBackgroundで保存しようとすると、imageDataの値を何度も上書きしています...保存すると、最後にimageDataに割り当てられました。 NSArrayを作成してそこに格納し、配列から引き出してviewDidLoadにしたいとします。

関連する問題