使用 CoreData 进行绘图 (CGPoint)

发布于 2024-11-14 01:42:31 字数 1548 浏览 5 评论 0原文

我想将 Core Data 用于绘图应用程序,其中用户使用 CGPoints 在 UIImageView 中绘图,可以选择保存并在稍后加载不同的绘图(如果用户需要,可以添加到它们 - 例如可变性)

我读过通过一些核心数据教程并浏览了 核心数据编程指南 但我似乎无法弄清楚需要做什么才能将绘图保存到核心数据,即使用什么类型的属性(我认为是可转换的)以及如何准确获取 CGPoints(我想再次转换)要使用我需要创建的托管对象。

要绘制我正在使用的方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {...}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {...}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {...}

iPhoneCoreDataRecipes 项目 Apple 将图像更改为 Core Data 的数据 - (因此您不必下载该项目,其用于转换的实现文件中的代码位于帖子底部)。在本例中,它将 PNG 转换为数据,因此我不确定如何将其应用于 UIImageView 中的 CGPoint。如果需要的话,我很乐意提供更多我的代码。

预先感谢,
詹姆斯

.

苹果的食谱代码:

@implementation Recipe
@dynamic name, image, overview, thumbnailImage, instructions, ingredients, type, prepTime;

@end

@implementation ImageToDataTransformer

+ (BOOL)allowsReverseTransformation {
return YES;
}

+ (Class)transformedValueClass {
return [NSData class];
}

- (id)transformedValue:(id)value {
NSData *data = UIImagePNGRepresentation(value);
return data;
}

- (id)reverseTransformedValue:(id)value {
UIImage *uiImage = [[UIImage alloc] initWithData:value];
return [uiImage autorelease];
}

@end

I would like to use Core Data for a drawing application where the user draws in a UIImageView using CGPoints has the option to save and at a later time load different drawings (and add to them if the user desires - e.g. mutability)

I've read through some cored data tutorials and skimmed the Core Data Programming guide but I can't seem to figure out what need to be done to save the drawing to Core Data i.e. what type of attribute to use (transformable I suppose) and how exactly to get the CGPoints (again transform I suppose) to work with the managedObject I'll need to create.

To draw I'm using methods:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {...}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {...}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {...}

In the iPhoneCoreDataRecipes project Apple changes the images to data for Core Data - (so you don't have to download the project their code in the implementation file for transforming is at the bottom of the post). In this case it's converting PNGs to data so I'm not sure how to apply this to CGPoints in a UIImageView. I'm happy to supply more of my code if necessary.

Thanks in advance,
James

.

.

Apple's Recipe code:

@implementation Recipe
@dynamic name, image, overview, thumbnailImage, instructions, ingredients, type, prepTime;

@end

@implementation ImageToDataTransformer

+ (BOOL)allowsReverseTransformation {
return YES;
}

+ (Class)transformedValueClass {
return [NSData class];
}

- (id)transformedValue:(id)value {
NSData *data = UIImagePNGRepresentation(value);
return data;
}

- (id)reverseTransformedValue:(id)value {
UIImage *uiImage = [[UIImage alloc] initWithData:value];
return [uiImage autorelease];
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2024-11-21 01:42:31

正如 TechZen 所建议的:

您是否需要查询 CoreData 中的坐标,还是只想获取绘图并绘制组成绘图的所有点?

如果您需要查询坐标,请创建一个代表每个具有 2 个属性(坐标)的 CGPoint 的核心数据实体,否则为每个具有可转换属性的绘图创建一个模型对象,并使用 NSValue 来存储所有点。

CG点-> NSValue ->; NSMutableArray ->; NSMutableArray -> (可转换)NSManagedObject 属性

As advised by TechZen:

Do you need to query for the coordinates in CoreData or do you only want to fetch a drawing and draw all points composing the drawing?

If you need to query for coordinates create a Core Data Entity representing each CGPoint with 2 attributes (coordinates), otherwise create a single Model Object for each drawing with a transformable attribute and NSValue to store all points.

CGPoint -> NSValue -> NSMutableArray -> (transformable) NSManagedObject attribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文