Saving a UIImage to NSUserDefaults - objective-c

So I'm trying to save an image taken from UIImagePickerController to NSUserDefaults so I can open it up in an album I generate, but when I call the delegate method to save the picture, I get the error:
[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<UIImage: 0x1d0bf0>' of class 'UIImage'. Note that dictionaries and arrays in property lists must also contain only property values.`
The code is as follows:
self.savedPic = [[UIImage alloc] init];
self.savedPic = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[[NSUserDefaults standardUserDefaults] setValue:self.savedPic forKey:[NSString stringWithFormat:#"image_%i",imageCount]];

You should save to disk your image and put the name of your image in the NSUserDefaults, then when needed create it :
[UIImage imageNamed:#"nameOfImage.png"]

It is quite simple, see my codes:
- (UIImage *)backgroundImage {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData* myEncodedImageData = [defaults objectForKey:#"backgroundImageData"];
UIImage* image = [UIImage imageWithData:myEncodedImageData];
return image;
}
- (void)setBackgroundImage:(UIImage *)image {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData* imageData = UIImagePNGRepresentation(image);
[defaults setObject:imageData forKey:#"backgroundImageData"];
[defaults synchronize];
}

Related

Accessing imageurl using userdefaults in objective c

I have fetched the image from image picker and uploaded the image to web service and i have a image url.I have saved the imageurl using user defaults as given below:
-(void)postSignupService {
[NewJsonHelperClass postExecuteWithParams:#"signup" secondParm:[self signUpParams] onCompletion:^(NSDictionary *json){
if ([[json valueForKey:#"status"] isEqualToString:#"success"]) {
NSDictionary *dataDict =[NSDictionary new];
dataDict =[json valueForKey:#"user"];
[[NSUserDefaults standardUserDefaults] setObject:#"sucess" forKey:#"LoginStatus"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"_id"] forKey:#"user_Id"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"userName"] forKey:#"userName"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"password"] forKey:#"Password"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"email"] forKey:#"email_Id"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"profile_pic"] forKey:#"image_Str"];
NSLog(#"datadict is %#",dataDict);
SelectFoodVC *selectVc = [appDelegateRef.storyBoard instantiateViewControllerWithIdentifier:#"SelectFoodVC"];
[self.navigationController pushViewController:selectVc animated:YES];
// TabBarController *tabVc = [appDelegateRef.storyBoard instantiateViewControllerWithIdentifier:#"TabBarController"];
// [self.navigationController pushViewController:tabVc animated:YES];
}
else{
[reUse showAlertWithTitle:[json valueForKey:#"status"] message:[json valueForKey:#"user"] thirdParam:self];
}
}];
}
-(NSDictionary *)signUpParams {
NSMutableDictionary *params =[NSMutableDictionary new];
params[#"userName"] =self.userNameTf.text;
params[#"email"] =self.emailTf.text;
params[#"password"] =self.passwordTf.text;
NSLog(#"imgstr is %#",imgStr);
params[#"profile_pic"] =imgStr;
return params;
}
Here profile pic value is imgStr which is got as imageurl from web service.
When i try to retrieve the imageurl in different viewcontroller,the value of userdefault is null as it shows error.
NSString *urlstring = [[NSUserDefaults standardUserDefaults] objectForKey:#"image_Str"];
NSLog(#"urlstring is %#",urlstring);
Here urlstring is null and the error is Error: Request failed: not found (404)
NSString *str = [NSString stringWithFormat:#"http://swiftdeveloperblog.com/wp-content/uploads/2015/07/1.jpeg"];
NSURL *url = [NSURL URLWithString:str];
[[NSUserDefaults standardUserDefaults]setURL:url forKey:#"URL"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSString *getURL = [[NSUserDefaults standardUserDefaults]URLForKey:#"URL"];
NSLog(#"url is %#",getURL);

How to include a mix of NSString and direct text input when setting an NSUserDefault?

I have a simple bit of code for which I can't seem to find the correct syntax:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *savedPrefix = [defaults stringForKey:#"stringPrefix"];
[defaults setObject:#"%#red_2-3.aiff" savedPrefix, forKey:#"myDefault"];
...So all I am trying to do is set 'myDefault' to (my NSString savedPrefix)+"red_2-3.aiff"
Can someone please help me adjust my syntax/method to make this work? Thanks!
You need to instantiate a NSString with stringWithFormat. Something like this will work:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *savedPrefix = [defaults stringForKey:#"stringPrefix"];
[defaults setObject:[NSString stringWithFormat:#"%#red_2-3.aiff", savedPrefix] forKey:#"myDefault"];
I prefer to break this a little more, like this: (it's the same thing, more granulated)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *savedPrefix = [defaults stringForKey:#"stringPrefix"];
NSString *stringToBeSaved = [NSString stringWithFormat:#"%#red_2-3.aiff", savedPrefix]
[defaults setObject:stringToBeSaved forKey:#"myDefault"];
[defaults setObject:[savedPrefix stringByAppendingString:#"red_2-3.aiff"] forKey:#"myDefault"];
[defaults setObject: [savedPrefix stringByAppendingString: #"red_2-3.aiff"]
forKey: #"myDefault"]

what does [[NSUserDefaults standardUserDefaults ] objectForKey] return?

if i'm doing this
NSArray * myArray = [[NSArray alloc] init];
myArray = [[NSUserdefaults standardUserDefaults] objectForKey:#"array"];
[myArray release];
am i destroying value stored in NSUserDefaults for key #"array"? Is it still extractable or not already? Does [[NSUserdefaults standardUserDefaults] objectForKey:#"array"]; return pointer or value?
Your call to objectForKey is returning an autoreleased array.
Don't do the alloc/init or release thing around that. You'd be leaking and most likely crashing.
Just do:
NSArray * myArray = [[NSUserDefaults standardUserDefaults] objectForKey: #"array"];
am i destroying value stored in NSUserDefaults for key #"array"?
NO
Is it still extractable or not already?
Extractable
Does [[NSUserdefaults standardUserDefaults] objectForKey:#"array"]; return pointer or value?
Pointer. Returns pointer to object.
Now your code is incorrect. You can rewrite it like this:
NSArray * myArray = [[[NSUserdefaults standardUserDefaults] objectForKey:#"array"] retain];
....
[myArray release];
It sounds to me like you want to get rid of the value. What you are doing is making an array with the data from [[[NSUserdefaults standardUserDefaults] objectForKey:#"array"].
If you want to get the data and use it elsewhere:
NSArray * myArray = [[[NSUserDefaults standardUserDefaults] objectForKey:#"array"] retain];
// do stuff here
[myArray release];
just like beryllium said. Of course, you could have to set it first:
[[NSUserDefaults standardUserDefaults] setObject:anObject forKey:#"array"];
If you want to get rid of the data:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"array"];
Because I honestly do not know what you are skiing, it is very hard to give you the answer you are looking for. Could you please clear it up a little?
Store array
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:yourArray forKey:#"yourKey"];
[userDefaults synchronize];
Retrieve
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *yourArr = [userDefaults objectForKey:#"yourKey"];

Objective-C save NSColor in NSUserDefaults

How do you save the color of the dot so that when the app is opened and closed the dot is the color it was last set to by the user?
Could someone explain to me how to use NSUserDefaults and in which methods to declare NSUserDefaults.
So far i have this:
NSData *data = [NSArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"MyColor"];
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:#"MyColor"];
NSColor *color = [NSUnarchiver unarchiveObjectWithData:data];
Link for tutorial I followed:
http://www.mactech.com/articles/mactech/Vol.25/25.04/2504RoadtoCode/index.html
This is what I use:
- (NSColor *)colorForKey:(NSString *)key
{
NSData *data;
NSColor *color;
data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
color= [NSUnarchiver unarchiveObjectWithData:data];
if( ! [color isKindOfClass:[NSColor class]] )
{
color = nil;
}
return color;
}
- (void)setColor:(NSColor *)color forKey:(NSString *)key
{
NSData *data = [NSArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
[BFChatWindow refresh];
}

how to convert nsdata to image?

I am developing an application in which I take NSData and convert it into image an display it in the image view. Now i want to save this data and display it again in UIImageView.
I used NSUserDefaults to save the NSData and again retrieve this data and convert into image and display it in image view.But i am not getting the image back.my code is as follows:
//for saving nsdata using nsuserdefaults method:-
NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults];
[defsMVC setObject:stateMVC forKey:#"MapImageObject"];
//for retrieving nsdata from nsuserdefaults and display it in imageview
NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults];
NSData *stringmapdata=[defsMVC dataForKey:#"MapImageObject"];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 6, 320,415)];
UIImage *image = [UIImage imageWithData:stringmapdata];
[imageView setImage:image];
[image release];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
[imageView release];
I don't think you should be storing the data in the user Defaults, try storing it in a file like "yourimage.png", then you can simply do [[NSImage alloc] initWithConentsOfFile:path];
Also try using NSArchiver before saving it in the user defaults:
NSImage *image = [NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:#"someKey"]];
And for archiving it to the user defaults:
[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:image] forKey:#"someKey"];
Gotta love Cocoa =)
My image is stored in 6th column in a form of NSData, first I have to get that data into NSData object. Than convert that NSData into image.
data = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 6)
length:sqlite3_column_bytes(statement, 6)];
img = [UIImage imageWithData:data];
imgdata = [[NSMutableArray alloc]initWithObjects:img, nil];