如何在核心数据中存储图像 [英] How to store an image in core data

查看:132
本文介绍了如何在核心数据中存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS的新手。我一直在试图做一个应用程序,将存储从相机捕获的图像 CoreData 。我现在知道如何存储数据,如 NSString s, NSDate 和其他类型,但努力存储图像。我读过这么多文章说你必须写入磁盘并写入文件,但我似乎不能理解。

I'm new to iOS. I've been trying to make an application that will store an image captured from the camera into CoreData. I now know how to store data like NSStrings, NSDate and other type but struggling to store an image. I've read so many articles saying you must write it to the disk and write to a file, but I can't seem to understand it.

以下代码我用来存储其他数据到核心数据。

The following code is the one i used to store other data to core data.

- (IBAction)submitReportButton:(id)sender
{
    UrbanRangerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    managedObjectContext = [appDelegate managedObjectContext];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"PotholesDB" inManagedObjectContext:appDelegate.managedObjectContext];
    NSManagedObject *newPothole = [[NSManagedObject alloc]initWithEntity:entity insertIntoManagedObjectContext:managedObjectContext];


    [newPothole setValue:self.relevantBody.text forKey:@"relevantBody"];
    [newPothole setValue:self.subjectReport.text forKey:@"subjectReport"];
    [newPothole setValue:self.detailReport.text forKey:@"detailReport"];
//    [newPothole setValue:self.imageView forKey:@"photo"];

    NSDate *now = [NSDate date];
    //NSLog(@"now : %@", now);
    NSString *strDate = [[NSString alloc] initWithFormat:@"%@", now];
    NSArray *arr = [strDate componentsSeparatedByString:@" "];
    NSString *str;
    str = [arr objectAtIndex:0];
    NSLog(@"now : %@", str);

    [newPothole setValue:now forKey:@"photoDate"];
    [newPothole setValue:self.latitudeLabel.text forKey:@"latitude"];
    [newPothole setValue:self.longitudeLabel.text forKey:@"longitude"];
    [newPothole setValue:self.addressLabel.text forKey:@"streetName"];
    [newPothole setValue:streeNameLocation forKey:@"location"];


    NSError *error;
    [managedObjectContext save:&error];

    UIAlertView *ll = [[UIAlertView alloc] initWithTitle:@"Saving" message:@"Saved data" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [ll show];
} 


推荐答案

使用二进制数据属性类型的数据。
不过,您应该注意以下几点:

You can store images in Core Data using the Binary Data attribute type. However you should be aware of a few things:


  • 始终将UIImage转换为便携式数据格式,如png或jpg
    例如:

  • Always convert your UIImage to a portable data format like png or jpg For example:

NSData * imageData = UIImagePNGRepresentation(image);

在此属性上启用允许外部存储

如果核心数据达到某个阈值,则会将数据移动到外部文件。此文件也完全由Core Data管理,因此您不必担心。

Enable "Allows external storage" on this attribute Core Data will move the data to an external file if it hits a certain threshold. This file is also completely managed by Core Data, so you don't have to worry about it.

如果遇到性能问题,数据属性到单独的实体。

If you run into performance issues, try moving the Binary Data attribute to a separate entity.

您应该将转换抽象为NSData的NSManagedObject子类的接口,所以您不必担心转换

You should abstract the conversion to NSData behind the interface of your NSManagedObject subclass, so you don't have to worry about conversions from UIImage to NSData or vice versa.

如果您的图片与模型中的实体没有很强的相关性,我建议将它们存储在Core Data之外。

If your images are not strongly related to the entities in your model, I would suggest storing them outside of Core Data.

这篇关于如何在核心数据中存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆