Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone:How can I store UIImage using NSUserDefaults?

I cannot save image using nsuserdefaults and crash my app and I also used custom class UserInfo which used

-(void)encodeWithCoder:(NSCoder *)encoder
{
   NSData *imageData = UIImagePNGRepresentation(categoryImage);
   [encoder encodeObject:categoryName forKey:@"name"];
   [encoder encodeObject:imageData forKey:@"image"];
}

-(id)initWithCoder:(NSCoder *)decoder and userInfo.categoryImage is UIImage variable.

reason: '-[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x7676a70'

userInfo = [[UserInfo alloc] init];

userInfo.categoryName = categoryName.text;
NSLog(@"Category Name:--->%@",userInfo.categoryName);

userInfo.categoryImage = image;

appDelegate.categoryData = [[NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 userInfo.categoryName, @"name",userInfo.categoryImage,@"image", nil] mutableCopy];


[appDelegate.categories addObject:appDelegate.categoryData];
NSLog(@"Category Data:--->%@",appDelegate.categories);

[appDelegate.categories addObject:userInfo.categoryName];
[appDelegate.categories addObject:userInfo.categoryImage];

[self saveCustomObject:userInfo.categoryName];

[self saveCustomObject:userInfo.categoryImage];


-(void)saveCustomObject:(UserInfo *)obj
{
    appDelegate.userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];


    [appDelegate.userDefaults setObject:myEncodedObject forKey:@"myEncodedObjectKey"];
}

So Please help me how can i store and retrieve uiimage using nsusedefaults???

like image 685
Nikunj Jadav Avatar asked Oct 01 '11 12:10

Nikunj Jadav


People also ask

What is NSUserDefaults?

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user's preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed.


1 Answers

If I were you, I'll save the UIImage into a file (use the [myImage hash]; to generate a file name), and save the file name as NSString using NSUserDefault ;-)

like image 84
PeakJi Avatar answered Sep 18 '22 23:09

PeakJi