1 2 Previous Next 15 Replies Latest reply: Feb 23, 2009 10:45 PM by Narender.mudgal
fireman888 Level 1 Level 1 (0 points)
Hi, everyone,

I am new to iphone programming. In my application, I really need to save an UIImage to NSUserDefaults, so next time user launch the app, the same image will be loaded. But I found I cound not save UIImage to NSUserDefaults. What is the work around?


2008-09-27 11:03:14.491 Js[4947:20b] * -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<UIImage: 0x45a220>' of class 'UIImage'.


Thank you very much.

fm

Mac mini, Mac OS X (10.5.4)
  • 1. Re: save UIImage to NSUserDefaults
    orangekay Level 5 Level 5 (4,085 points)
    Why don't you save a path to the image file instead of bloating up the prefs?
  • 2. Re: save UIImage to NSUserDefaults
    msv123 Level 2 Level 2 (175 points)
    NSUserDefaults is for simple things like strings, numbers, etc. To save a UIImage, save it as a file in your app's Documents directory.

    UIImage has the property "CGImage" of type CGImageRef which can get you the actual image data. CGImageRef is really a pointer to a CGImage object.

    A CGImage object has the method "representationUsingType:properties:" which returns an NSData object containing the image data in a PNG, JPEG, BMP, etc.

    Finally, NSData has "writeToFile:" methods that will save the data to a file.
  • 3. Re: save UIImage to NSUserDefaults
    fireman888 Level 1 Level 1 (0 points)
    Thank you for replying. My image comes from Photo Album selected by UIImagePickerController. My understanding is that the path is not available from UIImagePickerController. Am I wrong?

    Thank you very much.

    fm
  • 4. Re: save UIImage to NSUserDefaults
    fireman888 Level 1 Level 1 (0 points)
    thank you very much
  • 5. Re: save UIImage to NSUserDefaults
    fireman888 Level 1 Level 1 (0 points)
    You sure the "representationUsingType:properties" is in CGImage? I could not find it.

    Thank you very much.

    fm
  • 6. Re: save UIImage to NSUserDefaults
    msv123 Level 2 Level 2 (175 points)
    Sorry, my mistake, I was looking at a different class. Here's another way. I haven't tested this, but it should get you on the right track.

    UIImage *theImage;
    // ...
    CFDataRef dataRef = CGDataProviderCopyData(CGImageGetDataProvider(theImage.CGImage));
    const UInt8 *bytes = CFDataGetBytePtr(dataRef);
    CFIndex length = CFDataGetLength(dataRef);

    NSData *dataObj = [NSData dataWithBytesNoCopy:(void*)bytes length:(NSUInteger)length freeWhenDone:NO];
    [dataObj writeToFile:@"MySavedImage.bmp" atomically:NO];


    Check out the CGImage reference:
    http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Referenc e/CGImage/Reference/reference.html

    And this page under "How do I obtain raw pixel data...":
    http://developer.apple.com/iphone/library/codinghowtos/GraphicsAndAnimation/inde x.html#2D-OBTAINRAW_PIXEL_DATA_FROM_MYIMAGES
  • 7. Re: save UIImage to NSUserDefaults
    just.do.it Level 3 Level 3 (850 points)
    If you realy want to save an UIImage to NSUserDefaults you can use the NSKeyedArchiver class.

    NSData *imageData;

    // create NSData-object from image
    imageData = [NSKeyedArchiver archivedDataWithRootObject:yourUIImage];
    // save NSData-object to UserDefaults
    ...
  • 8. Re: save UIImage to NSUserDefaults
    kingsoul8 Level 1 Level 1 (10 points)
    This is very helpful to a project I am working on. I am very unfamiliar with saving and restoring settings. Would you mind showing the code that saves the NSData-object to UserDefaults.

    Thanks
  • 9. Re: save UIImage to NSUserDefaults
    just.do.it Level 3 Level 3 (850 points)
    Hi, that's quite simple:

    NSData *imageData;

    // create NSData-object from image
    imageData = [NSKeyedArchiver archivedDataWithRootObject:yourUIImage];
    // save NSData-object to UserDefaults
    [[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"image"];


    To load that image back on application-start use:

    NSData *imageData;

    imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
    if (imageData != nil) {
    yourUIImage = [NSKeyedUnarchiver unarchiveObjectWithData: imageData];
    }
  • 10. Re: save UIImage to NSUserDefaults
    kingsoul8 Level 1 Level 1 (10 points)
    Thank you so much. I was actually pretty close but my app keep terminating when my picture attempts to save. I must not be translating my image properly.


    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

    NSData *imageData;
    UIImage *theImage;
    imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
    if (imageData != nil) {
    theImage = [NSKeyedUnarchiver unarchiveObjectWithData: imageData];
    }
    }
    return self;
    }


    - (IBAction)openPicker {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
    UIImagePickerController *picker;
    picker = [[UIImagePickerController alloc]init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.allowsImageEditing = YES;
    picker.delegate = self;
    [self presentModalViewController:picker animated:YES];
    }
    }

    - (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingImage:(UIImage *)image
    editingInfo:(NSDictionary *)editingInfo {

    [self useImage:image];

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

    }


    -(void)useImage:(UIImage *)theImage {
    pickerImage.image = theImage;

    NSData *imageData;

    imageData = [NSKeyedArchiver archivedDataWithRootObject:theImage];
    [[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"image"];
    }

  • 11. Re: save UIImage to NSUserDefaults
    jerbeers Level 2 Level 2 (300 points)
    Are you getting an exception? What is the error you are getting?
  • 12. Re: save UIImage to NSUserDefaults
    kingsoul8 Level 1 Level 1 (10 points)
    After pressing "Choose" in the UIImagePickerController I go into perpetual "Saving photo" mode and never get out of it.
  • 13. Re: save UIImage to NSUserDefaults
    wurp Level 1 Level 1 (0 points)
    I ran across this page while trying to figure out how to save a UIImage to a png or jpg file, so I thought I'd post the two-line solution here that I finally found.


    //Save to PNG
    NSData *dataObj = UIImagePNGRepresentation(self);
    [dataObj writeToFile:path atomically:NO];



    //Save to JPG
    NSData *dataObj = UIImageJPEGRepresentation(self, 90);
    [dataObj writeToFile:path atomically:NO];


    Message was edited by: wurp

    Message was edited by: wurp
  • 14. Re: save UIImage to NSUserDefaults
    Xanthos Level 3 Level 3 (540 points)
    If you are running on the simulator that is expected behavior. The camera is not supported on the simulator.
1 2 Previous Next