Search code examples
iphoneiosuiimagepickercontrollermemo

How to persist values after a memory warning viewDidUnload


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).


Solution

  • 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.