Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > CoreData question, manage a NSImage programmatically?

CoreData question, manage a NSImage programmatically?
Thread Tools
black bear theory
Grizzled Veteran
Join Date: Aug 2005
Location: fairbanks AK
Status: Offline
Reply With Quote
Jun 20, 2007, 02:34 PM
 
i have a document-based coredata program that draws a series of lines on top of an image. it may sound weird but there's a reason for it.

i've created a bunch of entities to handle the lines and they are managed by a NSArrayControllers (set up in IB) and hooked into the "File Owner's" managedObjectContext. there isn't a problem with the data; i can open and close documents and all the data remains through the different operations.

however, i can't figure out how to manage the image. there is only one image per document and (possibly) many lines. adding the image each time you open a document is not ideal.

i have tried creating a NSObjectController in MyDocument.nib file and i can access the image via [_imageController content] after the image is set, however, the image isn't retained when a document is closed and reopened, though the data remains.

the thing i like about coredata is that all the data for my app is handled automatically, but i need some pointers on how to manage the one image for each document so that it is present each time i open the document.
Earth First! we'll mine the other planets later.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 20, 2007, 02:51 PM
 
Maybe I'm misunderstanding, but I'd just create an Image entity with a binary data attribute to store the image's data.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
black bear theory  (op)
Grizzled Veteran
Join Date: Aug 2005
Location: fairbanks AK
Status: Offline
Reply With Quote
Jun 20, 2007, 04:02 PM
 
i was thinking that might be the way, but i'm still having some trouble. i've set up the NSObjectController of entity "ImageMO".

i have this code in MyDocument.m:
Code:
-(void)awakeFromNib { NSLog(@"awakeFromNib"); NSImage *savedImage = [_imageController content]; if (savedImage) { NSLog(@"found some content"); [self setImage:savedImage]; } else { NSLog(@"no content available"); } } - (void)setImage:(NSImage *)value { if (image != value) { [image release]; image = [value copy]; [_view setImage:image]; [_imageController setContent:image]; } NSLog(@"now back in mydocument"); NSLog(@"image %@", image); NSLog(@"image content %@", [_imageController content]); }
and when i run the program, here is the output:

Code:
[Session started at 2007-06-20 11:54:58 -0800.] // 1 2007-06-20 11:54:58.876 Scanline[21485] awakeFromNib 2007-06-20 11:54:58.876 Scanline[21485] no content available // 2 2007-06-20 11:55:03.318 Scanline[21485] now back in mydocument 2007-06-20 11:55:03.318 Scanline[21485] image NSImage 0x3ec980 Size={1024, 768} Reps=( NSBitmapImageRep 0x3d8120 Size={1024, 768} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=24 Pixels=1024x768 Alpha=NO Planar=NO Format=0 ) 2007-06-20 11:55:03.319 Scanline[21485] image content NSImage 0x3ec980 Size={1024, 768} Reps=( NSBitmapImageRep 0x3d8120 Size={1024, 768} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=24 Pixels=1024x768 Alpha=NO Planar=NO Format=0 ) // 3 2007-06-20 11:55:07.277 Scanline[21485] awakeFromNib 2007-06-20 11:55:07.277 Scanline[21485] found some content 2007-06-20 11:55:07.277 Scanline[21485] now back in mydocument 2007-06-20 11:55:07.277 Scanline[21485] image NSImage 0x3ec980 Size={1024, 768} Reps=( NSBitmapImageRep 0x3d8120 Size={1024, 768} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=24 Pixels=1024x768 Alpha=NO Planar=NO Format=0 ) 2007-06-20 11:55:07.277 Scanline[21485] image content NSImage 0x3ec980 Size={1024, 768} Reps=( NSBitmapImageRep 0x3d8120 Size={1024, 768} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=24 Pixels=1024x768 Alpha=NO Planar=NO Format=0 ) // 4 2007-06-20 11:55:18.169 Scanline[21485] awakeFromNib 2007-06-20 11:55:18.169 Scanline[21485] no content available
i added the comments.

the above output is generated when i 1) create a new document 2) add the image 3) close and save the document and then 4) reopen the document.

i'm not sure why awakeFromNib is being called a second time when i am in the process of saving.

are there any obvious steps i've messed up? i know that _imageController is hooked up properly because i can see it.

i guess i can see one problem. even though imageController is of entity "ImageMO" i just call it's setContent:. i get errors if i try something like:

Code:
[_imageController setContent:image] // ok [_imageController setImage:image] // warning, doesn't function either
Earth First! we'll mine the other planets later.
     
black bear theory  (op)
Grizzled Veteran
Join Date: Aug 2005
Location: fairbanks AK
Status: Offline
Reply With Quote
Jun 21, 2007, 01:37 AM
 
Originally Posted by Chuckit View Post
Maybe I'm misunderstanding, but I'd just create an Image entity with a binary data attribute to store the image's data.
after struggling with this and then re-reading your post i figured out what you meant. :) i remembered doing something like this for a color well i had.

i added ImageMO:

@implementation ImageMO

- (void)awakeFromFetch {
NSData *imageData = [self valueForKey:@"imageData"];
if (imageData != nil) {
NSLog(@"ImageMO : attempting to load imageData : %@", imageData);
NSImage *image = [NSUnarchiver unarchiveObjectWithData:imageData];
[self setPrimitiveValue:image forKey:@"image"];
} else {
NSLog(@"ImageMO : no data found :(");
}
}

-(void)willSave {
NSImage *tmpImage = [self primitiveValueForKey:@"image"];
if (tmpImage != nil) {
NSLog(@"ImageMO : trying to save : %@", tmpImage);
[self setPrimitiveValue:[NSArchiver archivedDataWithRootObject:tmpImage] forKey:@"imageData"];
} else {
NSLog(@"ImageMO : no data to save : %@", tmpImage);
[self setPrimitiveValue:nil forKey:@"imageData"];
}

[super willSave];
}


- (NSImage *)image
{
NSImage *tmpValue;

[self willAccessValueForKey: @"image"];
tmpValue = [self primitiveValueForKey: @"image"];
[self didAccessValueForKey: @"image"];

return tmpValue;
}

- (void)setImage:(NSImage *)value
{
[self willChangeValueForKey: @"image"];
[self setPrimitiveValue: value forKey: @"image"];
[self didChangeValueForKey: @"image"];
}

- (BOOL)validateImage: (id *)valueRef error:(NSError **)outError
{
// Insert custom validation logic here.
return YES;
}

- (NSData *)imageData
{
NSData * tmpValue;

[self willAccessValueForKey: @"imageData"];
tmpValue = [self primitiveValueForKey: @"imageData"];
[self didAccessValueForKey: @"imageData"];

return tmpValue;
}

- (void)setImageData:(NSData *)value
{
[self willChangeValueForKey: @"imageData"];
[self setPrimitiveValue: value forKey: @"imageData"];
[self didChangeValueForKey: @"imageData"];
}

- (BOOL)validateImageData: (id *)valueRef error:(NSError **)outError
{
// Insert custom validation logic here.
return YES;
}

@end
setup/code is the same as above post (aside from some NSLog's taken out). _imageController is an entity of type ImageMO and bound to "File Owner's" managedObjectContext.

here is the output:
[Session started at 2007-06-20 21:28:52 -0800.]
2007-06-20 21:28:52.933 Scanline[25050] doc : no content available
2007-06-20 21:29:04.269 Scanline[25050] doc : found some content - NSImage 0x3d3e70 Size={1024, 768} Reps=(
NSBitmapImageRep 0x3e9a60 Size={1024, 768} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=24 Pixels=1024x768 Alpha=NO Planar=NO Format=0
)
2007-06-20 21:29:06.212 Scanline[25050] ImageMO : trying to save : NSImage 0x3d3e70 Size={1024, 768} Reps=(
NSBitmapImageRep 0x3e9a60 Size={1024, 768} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=24 Pixels=1024x768 Alpha=NO Planar=NO Format=0
)
2007-06-20 21:29:10.535 Scanline[25050] doc : no content available
2007-06-20 21:30:07.521 Scanline[25050] doc : no content available
awakeFromFetch doesn't seem to be called?
Earth First! we'll mine the other planets later.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 21, 2007, 02:38 AM
 
You need to call [super awakeFromFetch] before you do your initialization, I believe.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
black bear theory  (op)
Grizzled Veteran
Join Date: Aug 2005
Location: fairbanks AK
Status: Offline
Reply With Quote
Jun 21, 2007, 02:50 AM
 
no. that doesn't work. anyway, i should get the "no data found" message.

it's probably something simple. and stupid. it always is.

[edit: it was. ]
( Last edited by black bear theory; Jun 21, 2007 at 01:46 PM. )
Earth First! we'll mine the other planets later.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 07:58 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,