unable to save an NSMutableArray of image using persistent storage
-
11-06-2021 - |
سؤال
I am trying to store an NSMutableArray of images using persistive storage but it is not storing it
here is my code
[imagingList addObject:image ]; //image is of type UIImage
imagingHistory=[NSUserDefaults standardUserDefaults];
[imagingHistory setObject:imagingList forKey:@"imaging history" ];
and in my view did load I write
imagingList = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"imaging history" ];
when I re-load the application the list of objects is set back to Zero
am I missing something ?
المحلول
You can't save a UIImage directly in the user defaults. You have to convert it to an NSData first. Just by chance, when I was looking for the NSUserDefaults reference page to answer this question, a found a tutorial on how to do exactly what you want.
نصائح أخرى
Hmm you really shouldn't be trying to save UIImages into NSUserDefaults. Take a look at this resource https://discussions.apple.com/thread/1729849?start=0&tstart=0 it explains how to get the NSData of the image and write it to a file. From there you can save a reference (NSURL or NSString) to the image's path in your app's document directory and you can load it from there using something like UIImage aImage = [UIImage imageWithData:[NSData datWithContentsOfURL:pathURL]].
Hope that helps.