1

Possible Duplicate:
Size Limitation of NSUserDefault?

I am saving the images using NSUserDefaults. I want to know the maximum number of images i can save using NSUserDefaults or in other words how much memory we can use by using NSUserDefaults? Thanks

Community
  • 1
  • 1
Idrees Ashraf
  • 1,363
  • 21
  • 38

1 Answers1

3

If you are saving a number of images I would recommend saving images out the filesystem rather than saving them to NSUserDefaults (or CoreData for that matter) and then just saving the file name to NSUserDefaults.

This way there is no need to worry about any capacity restraints.

Saving the image:

UIImage* image = [UIImage imageNamed:@"example_image.png"];
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/saved_example_image.png"];
[imageData writeToFile:imagePath atomically:YES];

Retrieving the image:

NSString* imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/saved_example_image.png"];
UIImage* imageFromFileSystem = [UIImage imageWithContentsOfFile:imagePath];
Brendt
  • 397
  • 1
  • 9