iPhone:How can I store UIImage using NSUserDefaults?

12,568

Solution 1

UIImage does not implement NSCoding protocol, this is why you have the error.

Store:

NSData* imageData = UIImagePNGRepresentation(image);
NSData* myEncodedImageData = [NSKeyedArchiver archivedDataWithRootObject:imageData];
[appDelegate.userDefaults setObject:myEncodedImageData forKey:@"myEncodedImageDataKey"];
}

Restore:

NSData* myEncodedImageData = [appDelegate.userDefaults objectForKey:@"myEncodedImageDataKey"];
UIImage* image = [UIImage imageWithData:myEncodedImageData];

Solution 2

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 ;-)

Solution 3

Image Save on NSUserDefaults :

      [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:@"image"];   

Image retrieve on NSUserDefaults:

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
    UIImage* image = [UIImage imageWithData:imageData];
Share:
12,568
Nikunj Jadav
Author by

Nikunj Jadav

Sr. Sowftware Engineer in Ahmedabad,Gujarat,India

Updated on June 29, 2022

Comments

  • Nikunj Jadav
    Nikunj Jadav almost 2 years

    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???

  • Davyd Geyl
    Davyd Geyl over 12 years
    Make the UserInfo class implement 'NSCoding' protocol and archive the whole 'userInfo' object. Read this post of more info: stackoverflow.com/questions/3715038/… for example.
  • Nikunj Jadav
    Nikunj Jadav over 12 years
    check my update code and see its crash at line [encoder encodeObject:categoryImage forKey:@"image"]; so please help me
  • Davyd Geyl
    Davyd Geyl over 12 years
    You cannot encode the image, encode the data got from UIImagePNGRepresentation()
  • Nikunj Jadav
    Nikunj Jadav over 12 years
    thanks a lot I successfully encode image please check my updated code but app is crash [<NSCFString 0x75135b0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.' so please help me
  • Mugunth
    Mugunth over 12 years
    For heavens sake, please accept this as the answer. Saving a UIImage on NSUserDefaults is really a dumb idea.
  • Nico
    Nico over 12 years
    @NikunjR.Jadav: That's a different problem. Please ask a new question for that.
  • Lirik
    Lirik almost 9 years
    You should not do this line: [NSKeyedArchiver archivedDataWithRootObject:imageData]; You can save the data after UIImagePNGRepresentation. Also you need to synchronize the userDefaults after setting the object.