I have a database structure:UserDefaults->Empdata Dictionary->Iphone Array-> Dictionaries of each employee added. Along with Other details I want to save the image of the employee. The Employee uploads its image using ImagePicker. Also I want When all the details have been saved it can also be edited.I have thought of saving all the images in one folder and giving their image name as the name of the employee and then retrieving this image through the image name.If there is some other alternate please do tell. Any kind of help will be appreciable. Thanks in advance.
-
1An alternative called CoreData. NSUserDefaults is not a database, it meant to be used for app settings. – pronebird Sep 23 '14 at 12:09
-
Try to use `NSUserDefaults` for small data values only. For images, try to store images in documents directory. Using core-data for database based apps is best practice. – n00bProgrammer Sep 23 '14 at 12:10
-
I am new to ios i have no idea what to to do. I have saved other details in user defaults. If using document directory will solve my problem please suggest ho to implement it in my project. – Saksha Sep 23 '14 at 12:19
2 Answers
NSUserDefaults it's not intended to store binary data or large blobs of data! You should either save the image to disk or use Core Data to store it either to disk or in the database if it's smaller than 100KB.
EDIT:
To save image to disk from the ImagePickerController you could do something like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
NSString *imageFilename = [NSString stringWithFormat:@"%@.jpg", yourNameString];
NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString* imagePath = [NSString stringWithFormat:@"%@/%@", paths, imageFilename];
NSLog(@"IMAGE PATH: %@", imagePath);
[self dismissViewControllerAnimated:YES completion:^{
NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);
if([imageData writeToFile:imagePath atomically:YES])
{
NSLog(@"Write to Document folder success filename = %@",imagePath);
}
}];
}
For Core Data, you should search for some tutorials or books because it's much to learn and it is beyond the scope of this topic.

- 4,122
- 2
- 26
- 44
-
I am new to ios i have no idea what to to do. I have saved other details in user defaults. If using document directory will solve my problem please suggest how to implement it in my project. – – Saksha Sep 23 '14 at 12:24
-
-
It will solve what you've asked for. That is saving an image to disk with the name of an employee (see "yourNameString"). You also have the path to the image which you can add it to an NSDictionary containing info about your employee and later retrieve it from it. – Razvan Sep 23 '14 at 12:40
As everybody already suggested you should use application Directory to save and retrieve images.
Also instead of employee name use something uniques like employeeID.
Below post explains the process to save and retrieve in app directory. Do follow this:
Saving image to Documents directory and retrieving for email attachment