私は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];
}
:ここでは私が働いているコードがあります。これはおそらく簡単な修正ですが、どんな助けでも大歓迎です。ありがとう!
vdlData ...に保存された同じイメージを持つ各imageViewを割り当てていることに気付きましたか?そして、1つのパス(dataFilePath)に保存する前に、各イメージを同じ変数に保存しています – David
ああ、どうして私はそれを見逃しましたか?ありがとうございました。すべての変数を1つのパスに保存しても問題ありませんか、各変数ごとに異なるパスを設定する必要はありますか? – John
ここで私はそれを答えにします。 – David