Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

save UIImage to NSUserDefaults

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)

Posted on Sep 27, 2008 11:03 AM

Reply
15 replies

Sep 27, 2008 12:31 PM in response to fireman888

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.

Sep 27, 2008 5:23 PM in response to fireman888

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

Oct 21, 2008 10:41 AM in response to kingsoul8

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];
}

Oct 21, 2008 11:23 AM in response to just.do.it

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"];
}

Nov 20, 2008 7:26 AM in response to msv123

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

Feb 23, 2009 10:45 PM in response to just.do.it

Hi
I want to save my images data to database on the server side,
what should i do for that
after taking the picture
i will have one image thats what i know
how to deal with that and how to convert that to some string kind of data
so that i should pass that as parameter and save to the database on the server
and later after getting the data how to render that as image again
do you have any logic for that.
please help me , i dont have any idea how to encode and decode the image to some string or some thing. please mail me at narender.mudgal@gmail.com
thanks a lot

save UIImage to NSUserDefaults

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.