如何在 iOS 上将 cvMat 保存到 CoreData 中?
我在我的项目中使用 CoreData。我可以通过在我的项目中使用 UIImageToDataTransformer 类来保存 UIImage。另外,我正在使用 opencv 库,我需要保存一些 cvMat 结构(它们是结构,对吧?)。我读过一些关于可转换的文章,但仍然不知道如何将其保存在 coredata 中并取回。
我有一个名为“Human”的实体,它有一个名为 ProjVal 的属性,其类型是可转换的,它保存 cvMat 的数据。
我的下一步应该是什么?
下面是 UIImage 的转换器类,但如何转换 cvMat 的数据?感谢您的建议。
#import "UIImageToDataTransformer.h"
@implementation UIImageToDataTransformer
+ (BOOL)allowsReverseTransformation {
return YES;
}
+ (Class)transformedValueClass {
return [NSData class];
}
- (id)transformedValue:(id)value {
return UIImagePNGRepresentation(value);
}
- (id)reverseTransformedValue:(id)value {
return [[[UIImage alloc] initWithData:value] autorelease];
}
@end
编辑:使用其类型作为二进制数据类型而不是可转换类型是更好的方法吗?我为我的实体使用自动创建的类。
使用这段代码
CvMat *mat = cvCreateMat(3, 3, CV_32FC1);
for (int i=0; i<9; i++) {
mat->data.fl[i] = 1.0f;
}
NSData *projValData = [[NSData alloc] initWithBytes:&mat length:sizeof(mat)];
human.projVal = projValData;
我在保存和读回时
CvMat *mat;
NSData *data = [[humansArray objectAtIndex:0] projVal];
memcpy(&mat,[data bytes],[data length]);
if (mat) {
NSLog(@"if mat works");
for (int i=0; i<9; i++) {
NSLog(@"%f",mat->data.fl[i]);
}
}
:它不能很好地工作并且在 for 循环中崩溃。我的错误在哪里?
I am using CoreData in my project. I can save UIImage by using UIImageToDataTransformer class in my project. Also I am using opencv library and I need to save some cvMat struct (they are structs, right?). I have read some posts about transformable but still cant figure out how to save it in coredata and fetch it back.
I have one entity called "Human" and it has a attribute called ProjVal and its type is transformable which holds data of cvMat.
What should my next step be?
Below its the transformer class for UIImage but how can I transform cvMat's data? Thank for your advices.
#import "UIImageToDataTransformer.h"
@implementation UIImageToDataTransformer
+ (BOOL)allowsReverseTransformation {
return YES;
}
+ (Class)transformedValueClass {
return [NSData class];
}
- (id)transformedValue:(id)value {
return UIImagePNGRepresentation(value);
}
- (id)reverseTransformedValue:(id)value {
return [[[UIImage alloc] initWithData:value] autorelease];
}
@end
Edit: Is it a better way to use its type as Binary Data type instead Transformable type? Im using auto-created classes for my entities.
I use this code while saving
CvMat *mat = cvCreateMat(3, 3, CV_32FC1);
for (int i=0; i<9; i++) {
mat->data.fl[i] = 1.0f;
}
NSData *projValData = [[NSData alloc] initWithBytes:&mat length:sizeof(mat)];
human.projVal = projValData;
and while reading it back:
CvMat *mat;
NSData *data = [[humansArray objectAtIndex:0] projVal];
memcpy(&mat,[data bytes],[data length]);
if (mat) {
NSLog(@"if mat works");
for (int i=0; i<9; i++) {
NSLog(@"%f",mat->data.fl[i]);
}
}
it doesnt work well and crashed in for loop. Where is my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否获得指向 cvMat 数据的原始字节(及其大小)的指针?
如果是这样,您可以在您的
transformedValue:
实现中使用 NSData 的dataWithBytes:length:
类方法,并使用bytes
(实例)方法reverseTransformedValue:
。Can you get a pointer to (and size of) the raw bytes of your cvMat data?
If so, you can use the
dataWithBytes:length:
class method of NSData to in yourtransformedValue:
implementation and thebytes
(instance) method inreverseTransformedValue:
.