Harun Sagar
Harun Sagar

Reputation: 63

store image array in nsuserdefault

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.

Upvotes: 0

Views: 1779

Answers (3)

Shubhank
Shubhank

Reputation: 21805

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

Upvotes: 0

Mohammad Kamar Shad
Mohammad Kamar Shad

Reputation: 6067

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 

Upvotes: 1

hchouhan02
hchouhan02

Reputation: 946

try this

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

Upvotes: 1

Related Questions