1

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
jamesC
  • 422
  • 6
  • 25
  • Do you need to query for the coordinates in CoreData or do you only want to fetch an 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 transoformable attribute and NSValue to store all points. CGPoint -> NSValue -> NSMutableArray -> (transformable) NSManagedObject attribute – Martin Brugger Jun 08 '11 at 06:49
  • Hi Martin, I feel that I may only – jamesC Jun 09 '11 at 17:23
  • want to fetch the drawing that has been created (it will consist of 1-4 colors - maybe more) sorry if that throws you for a loop. So I want to store what has been drawn on the screen and then be able to retrieve it and either load the saved "picture" and/or email it. (I've seen apps that take a picture of the screen and add it to your photos so that you can email it at a later point). I understand, however, that sometimes it's better to adjust what I want to what is possible. Therefore, if saving it to Core Data is hairy turning it to a picture is ok. -j – jamesC Jun 09 '11 at 17:30
  • @Martin Brugger -- you should make your comment an answer so that it can be accepted. – TechZen Jun 09 '11 at 20:12

1 Answers1

1

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

Martin Brugger
  • 3,168
  • 26
  • 20
  • Hi Martin, as stated above I think in this case creating the MOM would be best. However i'm unclear about the: CGPoint -> NSValue -> NSMutableArray -> (transformable) NSManagedObject attribute process you mention and why that is preferable to apple's sample where they do an image to data transformer. cant I call .image on the ivar of UIImage. something like theImageview.image maybe a bit of code (or link to relevant code / or booktitle to demonstrate this process and I'd have a better understanding) Thanks- – jamesC Jun 10 '11 at 13:44