0

I have a single image that user picks from camera roll. Id like the user to have the facility to save and load the image from a UI Image view. I have image saved in a singleton class at the moment which is fine, until the app stops running or device is restarted.

Can I use something similar to the code i have used for storage a text string, for images? I have tried just changing .text to .image but unsurprisingly no such luck (it was a bit of a reach)

- (IBAction)functionsave:(id)sender {
NSString *savecontents = _function.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savecontents forKey:@"savecontents1"];
[defaults synchronize];


- (IBAction)Functionload:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:@"savecontents1"];
[_function setText:loadstring];
JSA986
  • 5,870
  • 9
  • 45
  • 91

1 Answers1

1

NSString objects are suitable to hold strings, not images. If you want to save images for later use I recommend you to save those images as PNG into your app's sandbox (i.e. documents folder):

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

You can save images by using:

UIImage *myImage = ...
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
[data writeToFile:[NSString stringWithFormat:@"%@/%@.png",  appDocsDirectory , @"myNewFile"] atomically:YES];

You can then retrieve images into an UIImage with:

UIImage* thumImage = [UIImage imageWithContentsOfFile:[[NSString stringWithFormat:@"%@/%@.png",  appDocsDirectory , @"myNewFile"]];

I think using real files is better for memory consumption than NSUserDefaults. Since you can load/unload from memory one file at a time.

Neo
  • 47
  • 4
  • Thank you for your guidance on this, sounds good, sorry if this is a very basic question...the save and retrive codes presumably is for my save and load buttons under IBAction? the top piece of code, is that in the .h of the file In the class I want to save and load the image? I keep getting errors when i put that in, "expected ";" at end of declaration list". Thanks – JSA986 May 04 '12 at 16:00
  • or Is it AppDelegate file in the app document directory I place this perhaps? As you have probably guessed im not very experienced in programming. – JSA986 May 04 '12 at 16:11
  • The top piece of code is the implementation code you need to fill-in the documentsDirectory object with the path to your applications document directory. The second piece of code should go at the save button under IBAction (remember to replace your UIImage at the "..."). The third example is for loading. Important note: That code is just an example, remember to cover cases where the image is not found/not present and NSErrors if writeToFile fails, etc. – Neo May 04 '12 at 16:11
  • Ok my documents directory seems to be in my Appdelegate.m #pragma mark - Application's Documents directory `// Returns the URL to the application's Documents directory. - (NSURL *)applicationDocumentsDirectory { return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; }` im not sure if this is right as im getting an undeclared identifier error – JSA986 May 04 '12 at 16:27
  • You can use that method to get the documents directory or just the one I've said before, it's not relevant. The AppDelegate is a good place to have system-wide methods (not that it is necessary to have them there, but it's common sense). If you have methods like that (and you import Appdelegate.h when needed), you can then call it with a single line like: [appDelegate applicationDocumentsDirectory]; – Neo May 04 '12 at 16:34
  • Hmmm ok staring to make more sense now, so final question how do Im not sure what i need to decalre where you have put ,appDocsDirectory, ive tried declaring "applicationDocumentsDirectory" from - (NSURL *)applicationDocumentsDirectory in appdeleagte .m but just get an error saying undeclared identifier, im not sure where or what to declare here...sorry about this – JSA986 May 04 '12 at 16:42