开发者

Using CoreData with drawing (CGPoint)

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)transforme开发者_如何学编程dValueClass {
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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜