How to save values โ€‹โ€‹after warning about memory warning DidUnload

My problem is when I use the UIImagePicker camera and the phone is out of memory. It issues a memory warning and unloads the view, and then reboots.

I know this is a very common problem, and many questions already exist in SO. I will not go into this.

- (void)viewDidLoad { [super viewDidLoad]; MyPersonClass *persons = [[MyPersonClass alloc] init]; persons.images = [NSMutableDictionary dictionaryWithCapacity:4]; .... ... } 

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

enter image description here

Sometimes it throws a popular memory warning error and unloads the view, and it removes all the images in the UIImageView that were made before the memory warning, and I lose everything in persons.iamges I just want to be able to get this data back. I do not know where to store it (I do not want to use CoreData for this).

+4
source share
3 answers

You can save instance variables to hold the image when calling viewDidUnload . set the flag to viewDidUnload and then save all the images of your images. And in viewDidLoad , check the box to assign saved images to ImageView when calling viewDidUnload .

+1
source

You can save your MutableDictionary as a plist in a sandbox application, for example

 [yourMutableDictionary writeToFile:pathToSave atomically:YES]; 

And then, get your NSMutableDictionary this way

 NSMutableDictionary *containOfPlist = [NSMutableDictionary arrayWithContentsOfFile:pathToYourPlist]; 

Hope this is what you want.

0
source

use NSCache to store images using a KeyValue pair. In the memory warning, write the NSCache object to a file and clear all objects from the NScache object in the low memory callback. when loading the application, rip the NSCAche from this file again.

therefore, in this way you can save the data and use it back.

another alternative is to take the NSDictnaory and save it using the NSArchiver class.

0
source

Source: https://habr.com/ru/post/1413691/


All Articles