【问题标题】:Compress Image data before saving to Core Data在保存到 Core Data 之前压缩图像数据
【发布时间】:2014-01-21 16:22:08
【问题描述】:

我正在创建一个学生索引应用程序,您可以在其中保存学生的姓名、图片和角色。一切正常,但是当我在应用程序测试时创建了一定数量的学生(带图片)时,应用程序运行速度非常慢。请查找随附的相应方法。有什么办法可以压缩图像数据的大小吗?

- (IBAction)cameraButtonPressed:(id)sender
{
    if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"no access to camera" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
        return;
    }
    UIImagePickerController *controller = [[UIImagePickerController alloc] init];
    controller.delegate = self;
    controller.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:controller animated:YES completion:nil];
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissViewControllerAnimated:YES completion:nil];    

    _imageView.image = image;
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
}

- (IBAction)save:(id)sender
{
        Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student"
                                                             inManagedObjectContext:self.managedObjectContext];

    self.student.picdata = UIImagePNGRepresentation(_imageView.image);

    [self.managedObjectContext save:nil];
    [self.delegate DetailStudentSavePressed:self];
}

【问题讨论】:

    标签: image performance core-data compression image-size


    【解决方案1】:

    已经压缩图像。当您调用UIImagePNGRepresentation 时,您会得到一个PNG 样式的压缩图像文件。

    您没有发布数据模型的详细信息,但至少,请确保 picdata 属性配置为在模型编辑器中使用外部存储。先这样做。

    如果这没有帮助,还有其他方法可以减少二进制 blob 对 Core Data 的影响。但这些都不是下一步。您特别提到了缓慢而不是内存问题,Core Data 和图像的问题更有可能导致内存问题。当您遇到速度问题时,不必担心图像处理,而是使用 Instruments 来分析您的应用程序。你会确切地知道它减速的地方。

    【讨论】:

    • 非常感谢您的回复。不幸的是,我无法为您的评论点赞,我需要再点赞才能被允许。
    猜你喜欢
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2021-01-03
    • 1970-01-01
    • 2010-12-16
    相关资源
    最近更新 更多