4

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

halfer
  • 19,824
  • 17
  • 99
  • 186
carbonr
  • 6,049
  • 5
  • 46
  • 73
  • That's why MVC approach works well in mobile applications. Data should be stored in Model (View or Controller is not a place to store data). There's another dirty trick - you can override viewcontroller's `didReceiveMemoryWarning` and leave it empty (ie: not calling `[super didReceiveMemoryWarning]`. But you didn't hear this from me :) Your app can be terminated if memory level is critical. – Rok Jarc May 22 '12 at 08:41
  • its no longer working in iOS 5. I already tried to overide but still the view unloads – carbonr May 22 '12 at 08:46
  • Thanks for the insight - didn't now that. Then keeping all your data in seperate class (object) would probably be the solution. – Rok Jarc May 22 '12 at 08:48

3 Answers3

1

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.

Hiren
  • 12,720
  • 7
  • 52
  • 72
Javal Nanda
  • 1,828
  • 3
  • 14
  • 26
0

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.

Edelweiss
  • 621
  • 1
  • 9
  • 24
0

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.

Prabhat Kasera
  • 1,129
  • 11
  • 28