I am getting an error in my viewDidLoad. It is saying there is a undeclared identifier ‘object’.
I’m new to this method of saving and retrieving data and I am not sure what to do.
//Viewcontroller.m
-(void)saveString:(UIImage*)myString
{
[[NSUserDefaults standardUserDefaults] setObject:myString forKey:@"image"];
}
- (IBAction)back:(id)sender {
[self saveString:object.image];
}
//ViewController2.m
-(UIImage*)retrieveString
{
UIImage* recoveredString = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
return recoveredString;
}
- (void)viewDidLoad
{
object.image = [self retrieveString];
[super viewDidLoad];
}
You can’t store an image in NSUserDefault, but you can store the path to it. So is not right to access NSUserDefault and pass the obeject for key image or viceversa. You can save it into your app sandbox and save the path to it. Then you can use class method
[UIimage imageWithContentsOfFile:path];By the way you can save it as a string of data char, but I don’t think it worth it and you need a proper encoding and decoding. You can also try to save it as an NSData object after a proper conversion, NSUserDefault supports it.