gpt4 book ai didi

ios - 将图像从 UIimageView 保存到 Core Data

转载 作者:行者123 更新时间:2023-11-29 03:00:26 25 4
gpt4 key购买 nike

我有一个“按钮”可以将相机胶卷中的图像加载到 imageView 中,效果非常好。现在我将图像保存在 imageView 我的数据库中。

我有 4 个“文本字段”,这没有问题。当我添加代码来保存“图像”时,应用程序崩溃了。

.h 文件中我有:

@property (weak, nonatomic) IBOutlet UIImageView *pictureData;

对于 UIImage

我的.m 文件:

- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];

if (self.device) {
// Update existing device
[self.device setValue:self.nameTextField.text forKey:@"name"];
[self.device setValue:self.versionTextField.text forKey:@"version"];
[self.device setValue:self.companyTextField.text forKey:@"company"];
[self.device setValue:self.adresse.text forKey:@"adresse"];
[self.device setValue:self.pictureData forKey:@"picture"];



} else {
// Create a new device
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
[newDevice setValue:self.nameTextField.text forKey:@"name"];
[newDevice setValue:self.versionTextField.text forKey:@"version"];
[newDevice setValue:self.companyTextField.text forKey:@"company"];
[newDevice setValue:self.adresse.text forKey:@"adresse"];
[newDevice setValue:self.pictureData forKey:@"picture"];


}

NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

[self dismissViewControllerAnimated:YES completion:nil];
}

xcdatamodeld(核心数据)中,我将图片作为二进制数据集类型!哪里出错了?

最佳答案

您应该保存其 image 属性,而不是保存 UIImageViewThis stack overflow question展示如何在 CoreData 中保存和检索 UIImage:

1)存储图像:

NSData* data = [NSData dataWithData:UIImagePNGRepresentation(yourUIImage)];
// save 'data' to core data

2) 载入图片:

// retrieve data from core data as per usual, stored in 'data'
UIImage* image = [UIImage imageWithData:data];

关于ios - 将图像从 UIimageView 保存到 Core Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23376064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com