how to save textcolor on textview by using NSUserdefaults - iphone-sdk-3.0

I am the beginner of iphone developer,my problem is how to save the textcolor on textview.
i want to taken 3buttons in each one set one color.when i tap the button.what the button had color text write on textview and at the same time save the feature also.when i open the application it does not lose the old feature.so how can solve the problem please help me.
thanks...

You can archive any object and store it in user defaults. To set it:
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:#"color1"];
And to retreive it:
NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:#"color1"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];

Related

How to Save and Load by iOS Programming

I have an application that I want to have take pictures using a UIImagePickerController, then stick said resulting image in a thumbnail view. But, when you exit the app, I want to re-display the same thumbnail as when the user last left off. My question is threefold:
What is the best way to implement saving and loading the image between app life cycles?
How would I go about changing the default shutter sound when the user takes a photo?
How can I reset all of the above to some default setting if the user so chooses?
Trying Using Core Data or NSUserDefault..
Some Links get you Started:
1.http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started
2.http://maniacdev.com/2012/03/tutorial-getting-started-with-core-data-in-ios-5-using-xcode-storyboards/
3. Some Code:
//Saving:
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image)forKey:key];
//Loading:
NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:key];
UIImage* image = [UIImage imageWithData:imageData];
what you can do at delegate you just save your image locally(I usually prefer sqlite) and when you start your app you just need to execute a select query to check whether the image exist or not and if it is, then just check which is latest one(or the case which you want to pick up).
Hope, it'll give you working idea.
In any concern, let me informed. :)
Either you save the image data to NSUserDefaults (as metioned by others) or your app's sandbox, or you might as well save the image to the device's camera roll in the didFinishPickingMediaWithInfo delegate like so:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
//Block Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[[info objectForKey:UIImagePickerControllerOriginalImage] CGImage] orientation:(ALAssetOrientation)[[info objectForKey:UIImagePickerControllerOriginalImage] imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(#"Error saving to Albums");
} else {
NSLog(#"Saved Image Url %#", assetURL);
}
}];
[library release];
The delegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info info dictionary object then has the image url for the key UIImagePickerControllerReferenceURL which you can then use to fetch the image, again using ALAssetsLibrary. HTH.

State of toggle switch in Settings Bundle

I have UIImageView what have a role of a background image in View. I want to load two different images in this UIImageView, depending on what state is selected in toggle switch in Settings Bundle.
What the best way to do it?
Try something like this:
NSUserDefaults* settings = [NSUserDefaults standardUserDefaults];
NSString* imgName = [settings boolForKey:#"toggle_switch"] ? #"switch_on.png" : #"switch_off.png";
UIImage* img = [UIImage imageNamed:imgName];
[imageView setImage:img];

Cache NSData(with UIImages)

Currently, I'm working on the client for the website. I have tonne of images that I need to load to my TableView. Here is what I'm currectly doing:
NSDictionary *propertyItems = [self.items objectAtIndex:indexPath.row];
cell.fio.font = [UIFont boldSystemFontOfSize:17.0f];
if([propertyItems objectForKey:#"online"] == [NSNumber numberWithInt:1])
cell.fio.textColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.3 alpha:1.0];
dispatch_queue_t downloadPhotosQueue = dispatch_queue_create("downloadPhotosQeue", NULL);
dispatch_async(downloadPhotosQueue, ^{
NSData *photosData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/%#", [propertyItems objectForKey:#"photo_trumb"]]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.pic.image = [UIImage imageWithData:photosData];
});
});
cell.fio.text = [propertyItems objectForKey:#"fio"];
I'm doing this in cellForRowAtIndexPath: method.
Everything is loading fast. But the problem is, when I'm scrolling my table down, and after again up, the images are reloading over and over again.
Question: Is there any way to easily cache my UIImages that I'm getting from the server? So if they are loaded once, they wouldn't reload over and over again, while I'm running the app. Maybe I'm doing something wrong?
Thanks in advance!
I strongly recommend AsyncImageView. I use it and it works like a charm. Just set the image url and it handles everything itself.
AsyncImageView *imageView = [[AsyncImageView alloc]init];
imageView.imageURL = [NSURL URLWithString:#"https://www.google.es/logos/classicplus.png"];
It caches the image in memory so it won't retrieve it from the server again. It will also release them when receiving a memory warning.
To load image from a website I use AFNetworking. It provides a class to load an image from an URL:
Start by adding #import "UIImageView+AFNetworking" to the top of a
controller or a view implementation file. This category adds methods
to UIImageView, like:
[imageView setImageWithURL:[NSURL URLWithString:#"…"]];
i recommend this library to accomplish what you want.
in cellForRowAtIndexPath:
[cell.imageView setImageWithURL:
[NSURL URLWithString:[NSString stringWithString:#"www.yourimagepath.path"]]
placeholderImage:[UIImage imageNamed:#"no_image.jpg"]];
the library will cache the image for you. you can also set the setHolder of the image, so it wont look like a blank image while the images are downloading.
The problem is here :
NSData *photosData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/%#", [propertyItems objectForKey:#"photo_trumb"]]]];
dataWithContentsOfURL
It launch a request to the Url everytime the function is called (everytime you scrolled).
You need to load all the picture before cellForRowAtIndexPath.
Like in ViewDidLoad.
You can just store them in an array and display your picture's array in cellForRowAtIndexPath.
If it's really fast to load like you say : jsut load picture Once in CellForRowAtIndexPath. Store them in a mutableArray. And check if the picture already exist .

Font and Font Size Changing

I have a Mac app with a plain-text text view. I have been able to get it to save, etc, but when I change the font or font size in my app using the font panel, it is not saved when the app re-opens.
How can I create a way that my app will save the font and font size changes? Thanks!
Save Font and Font Size in NSUserDefaults and load from NSUserDefault at the time of application launching.
NSString *valueToSave = #"someValue";
[[NSUserDefaults standardUserDefaults]
setObject:valueToSave forKey:#"preferenceName"];
to get it back later
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"preferenceName"];
Take a look at Save string to the NSUserDefaults? post.

How do you save a picture with NSUserDefaults?

I have a iPad app that can take a picture and then put it within a image view. How do I go about saving the pic that I have stored in the image view? Here is what I have tried so far: [imageView setImage:[[NSUserDefaults standardUserDefaults] objectForKey:#"storedImageValue6"]];
Pretty much you have to convert it to NSData and vice versa. Take a look at this link: Save images in NSUserDefaults?
Here imageSet is UIImage
NSData *imagedata=UIImagePNGRepresentation(imageSet);
[[NSUserDefaults standardUserDefaults] setObject:imagedata forKey:#"Theme Selecting"];
[[NSUserDefaults standardUserDefaults] synchronize];
You can use this code for saving image