Reputation: 1471
I save picture from UIImagePicker
this way:
Save picture in file and then I save path to the fail in NSUserDefaults
and then in another class I retrieve the picture by this saved path.
Code:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
ideaImage.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[picker dismissModalViewControllerAnimated:YES];
}
-(void)saveIdea_alt
{
[self performSelector: @selector(saveIdea) withObject:nil afterDelay:0.1];
}
-(void)saveIdea
{
UIImage *ideaPhoto = ideaImage.image;
NSData *imageData = UIImagePNGRepresentation(ideaPhoto);
NSString* imageName = @"MyImage.png";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
[imageData writeToFile:fullPathToFile atomically:NO];
NSArray *arrayKeys = [[NSArray alloc]initWithObjects:@"ideaName",@"ideaCost",@"ideaNote", @"ideaImage", nil];
NSArray *arrayObjects = [[NSArray alloc]initWithObjects:ideaName.text,ideaCost.text,ideaNote.text,fullPathToFile , nil];
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjects:arrayObjects forKeys:arrayKeys];
NSMutableArray *ideasArray = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"ideasArray"]];
[ideasArray addObject:dictionary];
[[NSUserDefaults standardUserDefaults]setObject:ideasArray forKey:@"ideasArray"];
[self dismissModalViewControllerAnimated:YES];
}
However, after that my app becomes slow and it saves and loads images slowly. What I do wrong ? Thanks !
Upvotes: 0
Views: 262
Reputation: 672
One interesting optimization to look at is not saving it into NSUserDefaults, and using Core Data. There are a lot of reasons for this, but one important one is that when you want to add and remove things from NSUserDefaults, the app has to load all of NSUserDefaults into memory just go retrieve one key.
Upvotes: 2