gpt4 book ai didi

objective-c - 将图像保存到核心数据

转载 作者:可可西里 更新时间:2023-11-01 03:39:29 26 4
gpt4 key购买 nike

我是核心数据相关的新手。任何人都可以帮助我提供正确的步骤/教程,展示如何将图像保存到核心数据及其检索。我已经能够存储字符串数据,但是我的应用程序在尝试保存图像时崩溃了。保存:

DataEvent *event = (DataEvent *)[NSEntityDescription insertNewObjectForEntityForName:@"DataEvent"
inManagedObjectContext:managedObjectContext];
NSURL *url2 = [NSURL URLWithString:@"xxxxxxxxxxxxxxx SOME URL xxxxxxxxxxxx"];

NSData *data = [[NSData alloc] initWithContentsOfURL:url2];
imageSave=[[UIImage alloc]initWithData:data];
NSData * imageData = UIImageJPEGRepresentation(imageSave, 100.0);
[event setValue:self.imageSave forKey:@"pictureData"];

对于检索:

 DataEvent *event = (DataEvent *)[eventsArray objectAtIndex:indexPath.row];
UIImage *image = [UIImage imageWithData:[event valueForKey:@"pictureData"]];
UIImageView *imageViewMainBackGround = [[UIImageView alloc]
CGRect rect3=CGRectMake(0,2,100.0,100.0);
imageViewMainBackGround.frame = rect3;
[cell.contentView addSubview:imageViewMainBackGround];
[imageViewMainBackGround release];

最佳答案

保存:

NSData *imageData = UIImagePNGRepresentation(myUIImage);

[newManagedObject setValue:imageData forKey:@"imageKey"];

和检索图像:

NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:@"imageKey"]];
[[newCustomer yourImageView] setImage:image];

改变格式

关于objective-c - 将图像保存到核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10311271/

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