Quote:
Originally Posted by jcc
Hi,
I am using NSCoder to save various images, text, and drawings supplied by the user and saving it into NSUserDefaults like so....
[code]
- (void)encodeWithCoder  NSCoder *)coder {
}
- (id)initWithCoder  NSCoder *)coder {
self = [super initWithCoder:coder];
return self;
}
- (void) saveCurrentSetup {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject: imageView] forKey:@"image_View"];
.....etc.
}
[code]
Now I am starting to second guess the idea of saving these objects to NSUserDefaults because it's quite a high memory load.
There are only certain views that actually use the saved objects. So I don't really need them taking up memory all the time. Is there a way to manage these different objects so that I can call them into memory only when they are needed?
Thanks.
|
First:
Your class isn't really implementing the NSCoding protocol if your initWithCoder and encodeWithCoder methods simply call super. You need to write code in those methods that encodes/decodes the properties and iVars that you need in order to reconstruct your object.
Now, you have a method saveSetup that uses the NSKeyedArchiver class method archivedDataWithRootObject to convert an image view to a data object.
Why do you want to encode the image view? Do you want to be able to load the view from user defaults and add it as a subview of some view controller?
I would think it would make more sense to encode the image that's inside the image view, and then write (perhaps in your view controller's viewDidLoad method) that loads the image and installs it into an image view that was loaded by your nib file.
Saving images to user defaults should be ok. I don't think user defaults loads everything into memory at once. As far as I know, it only loads objects into memory when you ask for them, so your memory footprint should be reasonable.
As far as only loading your images when you need them, sure. That's a good idea. However, only you know when you need them, since you're the one writing your program.
If they are images that are displayed in views in view controllers, only load the images when the view controller's viewDidLoad method is called, and simply install the images in their image views. Then, if there is a low memory alert, the system will unload views that aren't in use and the images will be released from memory.