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

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

我真的不想使用核心数据,因为我已经保存了想法的所有其他属性NSUserDefaults(如成本、名称、颜色等)

4

2 回答 2

1

在设置值后NSUserDefaults,只需写下这一行:

[[NSUserDefaults standardUserDefaults] synchronize];

希望这会有所帮助。

于 2012-06-20T07:17:15.357 回答
0

您是否尝试过使用

[self performSelectorInBackground: @selector(saveIdea)  withObject:nil];

代替

[self performSelector: @selector(saveIdea)  withObject:nil afterDelay:0.1];
于 2012-06-20T08:00:09.873 回答