iphone - 保存和检索图像会使应用变慢

标签 iphone objective-c ios xcode uiimage

我以这种方式从 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 中,而是使用 Core Data。 这有很多原因,但一个重要的原因是当你想在 NSUserDefaults 中添加和删除内容时,应用程序必须将所有 NSUserDefaults 加载到内存中,只需检索一个键即可。

关于iphone - 保存和检索图像会使应用变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11106017/

相关文章:

iOS推送通知-如何指定通知声音

iphone - 应用委托(delegate)对象

iphone - 使用 AVAssetReader 绘制波形

ios - 如何在向后导航中获取 View Controller Storyboard ID 或 restortionIdentifier?

ios - 如何使用 Parse Swift iOS 8 Xcode 6.4 注销

ios - 设备受到 TouchEnded 调用的轰炸

iphone - 如何使用 iPhone 追踪 friend 的行踪?

ios - 核心数据测试 : force external storage on iOS

objective-c - UITableViewCell 子类在所有 super 方法上都出现构建错误

ios - 带有半透明导航栏的状态栏背景颜色