store image array in nsuserdefault - iphone

I have created an ios application where i need to store image array into nsuserdefaults. When restart the app again the storing image need to show in a scroll view. How can i store image array and get the array from nsuserdefault . Thank in advance.

try this
NSData *dataVal = [NSKeyedArchiver archivedDataWithRootObject:val];
[defaults setObject:dataVal forKey:keyName];
[defaults synchronize];

Try This
for(int val=0;val<[imageArray count];val++){
// imageArray is an Array which contains the Image Names
// See below converting the
NSData *imgData = UIImagePNGRepresentation([UIImage imageNamed:[NSString stringWithFormat:#"#%d",[imageArray objectAtIndex]val]]);
//add this `imgData` into dataArray(Which is An NSmutableArray don't forget to Allocate it)
[dataArray addObject:imgData];
//then Store this Array to NSuserDefaults
[[NSUserDefaults standardUserDefaults] setObject:dataArray forKey:#"key"];
}
// the Get the Stored Data Array from NSuserDefaults.
NSMutableArray* storedDataArray=[[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:#"key"]];
//`storedDataArray` array contains the ImageData(NSData)
//Use it As you want

NSData *imgData = UIImageJPEGRepresentation(YOURIMAGE,1.0);
// Now store the NSData in the defaults for a key
then load it like this
UIImage *image = [UIImage imageWithData:[defaults objectForKey:#"Image"]];

Related

NSArray to NSData for Core Data

I need to save array in core data, so i read that I can use NSData for it. So I think that I have problem with archiving.
NSMutableArray *newArray = [[NSMutableArray alloc]init];
NSData *newData = [NSKeyedArchiver archivedDataWithRootObject:newArray];
[myEntity setValue:newData forKey:#"nameOfMyData"];
And then I try to pick my array in another VIewController for filling
NSData *newdata = [NSData dataWithData:self.myEntity.nameOfMyData];
NSMutableArray *photoArray = [[NSMutableArray alloc]init];
photoArray = [NSKeyedUnarchiver unarchiveObjectWithData:newdata];
I have no crash, but in command line appear next:
[NSKeyedUnarchiver initForReadingWithData:]: data is empty;
did you forget to send - finishEncoding to the NSKeyedArchiver?
And when i try to add object to my array, it does not add
[photoArray addObject:myImage];
So myImage is creating and with it I have no trouble, but in debugger always write for photoArray:
photoArray = (NSMutableArray*) 0x00000000 0 objects
It should work. But when unarchiving the array rather use:
NSData *newdata = [NSData dataWithData:self.myEntity.nameOfMyData];
NSMutableArray *photoArray = [NSMutableArray arrayWithArray: [NSKeyedUnarchiver unarchiveObjectWithData:newdata]];
You either have objects in your array that dont conform to NSCoding protocol or you do not save your core data context (or saving is unsuccessful).
After archiving with
NSData *newData = [NSKeyedArchiver archivedDataWithRootObject:newArray];
what do you see when you check newData for nil? I suppose it is nil.

My information in a plist isn't saved

I have some settings in a plist, but when I kill my app I lose all the data stored there.
This is the code that I'm using:
.h
#property (retain, nonatomic) NSString *plistFilePath;
-(IBAction)setHomepage:(id)sender;
.m
#syntehzise plistFilePath;
-(IBAction)setHomepage:(id)sender{
plistFilePath = [NSString stringWithString:[[NSBundle mainBundle] pathForResource:#"settings" ofType:#"plist"]];
NSMutableDictionary *data= [[NSMutableDictionary alloc] initWithContentsOfFile:plistFilePath];
[data setObject:#"http://www.google.com" forKey:#"Homepage"];
[data writeToFile:plistFilePath atomically:YES];
[data release];
}
Am I doing something wrong? Should I use a different class or differents methods? Please help me because I don't know why I store well the information but then when I kill the app I lose it.
As already mentioned the bundle is read only.
Try to avoid setting 'settings' in an a copied plist, as plists are just one more thing to managed. Instead, why not use NSUserDefaults and import your defaults from a defaults plist. For example, add a new plist to your project, and add this to your delegate:
// Get the shared defaults object
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Register the defaults each time the app loads
NSString *defaultsFile = [[NSBundle mainBundle] pathForResource:#"Defaults" ofType:#"plist"];
NSDictionary *defaultsDict = [NSDictionary dictionaryWithContentsOfFile:defaultsFile];
[defaults registerDefaults:defaultsDict];
Now you can save data like this:
// Store the data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"http://mattmelton.co.uk" forKey:#"HomePage"];
[defaults synchronize];
And retrieve it like this:
// Retrieve data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *homePage = [defaults objectForKey:#"HomePage"];
And you don't have to worry about external files. Naturally your defaults plist can be platform, user or device specific!
Hope this helps!
The application bundle is readonly. If you want to distribute a file and then update it, move it from the bundle to the documents folder the first time your application runs.

How to store images in cache?

in iphone i have requirement of store image in cache .
but how i can store this in cache.
I know the folder path of cache. is this only way i have to WRITE my image in that folder for cache?
I want to know any other way. or is this correct way?
Please help me.
You can store data on local disk using NSArchiver and NSUnarchiver in this way:
// set your image in cache
NSData *imgData = [NSKeyedArchiver archivedDataWithRootObject:myImage];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:imgData forKey:#"img_01"];
[defaults synchronize];
//get your image from cache
NSData *imgData = [[NSUserDefaults standardUserDefaults] objectForKey:#"img_01"];
myImage = [NSKeyedUnarchiver unarchiveObjectWithData:imgData];
Reference apple here: http://goo.gl/XH2o2
hope this helps.
use this link..
http://www.makebetterthings.com/iphone/image-caching-in-iphone-sdk/
edited:
get file path using this code and fatch the particular file :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];

iPhone:Convert NSMutableArray to NSData with stored & retrieved issue

I am facing a problem, that is to convert NSMutableArray into NSData and stored & retrieved that array into rootviewcontroller so please give me idea. Here is the code I'm working with:
appDelegate = (FindNearMeAppDelegate *)[[UIApplication sharedApplication]delegate];
appDelegate.isCategoryCell = TRUE;
appDelegate.isButton = NO;
NSString *categoryStr = categoryName.text;
NSLog(#"Category Name:--->%#",categoryStr);
appDelegate.categoryData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
categoryStr, #"name",image ,#"image", nil];
[appDelegate.categories addObject:appDelegate.categoryData];
NSLog(#"Category Data:--->%#",appDelegate.categories);
I am initialize nsmutablearray (appDelegate.categories) and nsmutabledictionary in appdelegate.
Thanks in advance.
From what I understand, you want to save your images and retrieve them. You can convert your image to NSData using one of the 2 methods, UIImagePNGRepresentation or UIImageJPEGRepresentation depending on the type of the image. You can then save this data-
NSData* imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:imagePath atomically:YES]; //imagePath is where you want to save it. Most likely it contains the value of the name key in your dictionary.

What's the easiest way to persist data in an iPhone app?

I want to persist a very simple string. For example "abc". What's the easiest way to do this? I don't want to use a SqlLite database.
If it is just one single string NSUserDefaults is probably the easiest way.
// write
[[NSUserDefaults standardUserDefaults] setObject:#"abc" forKey:#"MY_PERSISTENT_KEY"];
// read
NSString *abc = [[NSUserDefaults standardUserDefaults] valueForKey:#"MY_PERSISTENT_KEY"];
You can make your objects conform to NSCoding, then write them to a file using an NSKeyedArchiver.
NSString* path = [self pathForDataFile];
NSMutableDictionary* rootObject = [NSMutableDictionary dictionary];
[rootObject setValue: someObject forKey: #"SomeObject"];
// Commit data to file
[NSKeyedArchiver archiveRootObject: rootObject toFile: path];