当设备关闭时,如何停止文本字段,并且所选择的图像消失

问题描述 投票:0回答:2

如果我的应用程序停止运行或设备关闭,则我从文本选择器中提取的文本字段和图像全部重置为空白,如何保留此信息?

我使用了一个单身人士(在一位成员的帮助下),我可以保留我的图像...直到应用被杀死或设备关闭。然后它消失了。

.m
- (void)viewDidLoad
{
singletonObj = [Singleton sharedSingletonController];
imageView.image = singletonObj.imagePicked;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{

[self setImageView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:              (UIInterfaceOrientation)interfaceOrientation
 {
 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
 }

 #pragma mark - Action





- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}

- (IBAction)btn:(id)sender {



    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if((UIButton *) sender == choosePhotoBtn) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }

    [self presentModalViewController:picker animated:YES];


    }

 -(void)imagePickerController:(UIImagePickerController*)picker   didFinishPickingMediaWithInfo:(NSDictionary*)info

{


NSData  *dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);

UIImage *img = [[UIImage alloc] initWithData:dataImage];
singletonObj.imagePicked = img;
imageView.image = img;
[picker dismissModalViewControllerAnimated:YES];



}
@end
ios xcode singleton photo
2个回答
1
投票

有两种类型的存储器:易失性(RAM)和永久性存储器(即:硬盘驱动器和其他存储器)。

程序/计算机关闭时易失性存储器已清除并丢失。

使用单例很好,但是与保持会话之间的数据完全无关(而会话我的意思是程序正在运行的时间:从应用程序的启动到终止)。

您需要使用任何所需的方法,将希望保留在会话之间的数据存储在文件之间。根据要存储的信息,有不同的专用保存机制:

  • ((例如NSUserDefaults用于用户首选项)。
  • Core Data是一个框架,定义用于构造数据并将其保存/读取到文件(也称为持久性存储)的机制。
  • 您也可以使用serialization
  • 或者您始终可以手动操作文件。
  • NSData具有NSData,它将根据数据对象写入创建文件。如果要保存图像,则必须获取UIImage的基础数据(即writeToFile:atomically:)。

0
投票

您将不得不使用核心数据。它可以接受来自UIIimage的NSData以及NSStrings。 appDelegate中的函数AppicationWillTerminate必须使用,以便在终止应用程序之前就可以存储信息。

这将需要一定数量的工作才能使其正常工作,但没有什么困难。如果您需要帮助来了解核心数据,建议您使用此链接

writeToFile:atomically:

© www.soinside.com 2019 - 2024. All rights reserved.