0

UIImagePicker我通过这种方式保存图片:

将图片保存在文件中,然后我保存失败的路径,NSUserDefaults然后在另一个类中,我通过这个保存的路径检索图片。

代码:

-(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];
}

但是,在那之后我的应用程序变慢了,它保存和加载图像的速度很慢。我做错了什么?谢谢 !

4

1 回答 1

2

一项有趣的优化是不将其保存到 NSUserDefaults 中,而是使用 Core Data。这有很多原因,但一个重要的原因是,当您想从 NSUserDefaults 添加和删除内容时,应用程序必须将所有 NSUserDefaults 加载到内存中,只需检索一个键即可。

于 2012-06-19T17:22:49.367 回答