How to persist values after a memory warning viewDidUnload

300 views Asked by At

My problem is when I use the camera UIImagePicker and the phone is low on memory. It throws a memory warning and unloads the view and then reloads.

I know this is a very common problem and many questions are already in SO. I'm not going into this.

- (void)viewDidLoad
{
    [super viewDidLoad];

    MyPersonClass *persons = [[MyPersonClass alloc] init];   
    persons.images = [NSMutableDictionary dictionaryWithCapacity:4];
....
...
}    

My issue is the my view controller has four buttons and UIImageViews and when I tap each, they open the camera and image clicked is shown back in the UIImageViews and these images are also stored in the persons.images NSMutable Dictionary.

enter image description here

Sometimes it throws the popular error of memory warning and Unloads the view and this removes all the images in the UIImageView which were taken before the memory warning and I lose everything in persons.iamges I just want to be able to retrieve this data back. I don't know where to store it (I don't want to use CoreData for this).

3

There are 3 answers

3
Javal Nanda On BEST ANSWER

You can maintain instance variables for storing image when viewDidUnload gets called. set a flag in viewDidUnload and then store all the images of your Imageviews. And in viewDidLoad check the flag that if viewDidUnload was called then assign the saved images to your ImageView.

0
Edelweiss On

You can save your MutableDictionary as a plist in the sandbox's application like that

[yourMutableDictionary writeToFile:pathToSave atomically:YES];

And then, get your NSMutableDictionary by this way

NSMutableDictionary *containOfPlist = [NSMutableDictionary arrayWithContentsOfFile:pathToYourPlist];

Hope this is what you want.

0
Prabhat Kasera On

please use the NSCache for storing the images with KeyValue pair. On the memory warning write the NSCache object to file and clear all objects from NScache object on low memory warning call back. on loading application again rad the NSCAche from that file.

so i that manner you can save the data and can use it back.

another alternate option is to take NSDictnaory and store it via using NSArchiver class.