Trouble Fetching NSImage data through bindings
Trouble Fetching NSImage data through bindings
- Subject: Trouble Fetching NSImage data through bindings
- From: Arved von Brasch <email@hidden>
- Date: Sun, 24 Sep 2006 16:07:46 +1000
Hello Cocoa Dev List,
I'm having trouble using bindings to fetch NSImage from a persistent
store. I separated out the image data into its own object as
recommended. My CoreData model looks like this:
Entity: Preview
picture (Binary data)
preview (Undefined/transient - NSImage)
scaledPreview (Undefined/transient - NSImage)
file (Relationship)
Entity: File
title (String)
preview (Relationship)
They are in a one-to-one relationship. The class implementation is
like so:
- (void)awakeFromInsert {
[super awakeFromInsert];
[self setValue: [NSArchiver archivedDataWithRootObject: [NSImage
imageNamed: @"nopicture.png"]] forKey: @"picture"];
}
float heightDefault = 250.0;
float scaledDefault = 40.0;
- (NSImage *)scaleImage: (NSImage *)image toHeight: (float)height {
NSSize size = [image size];
NSSize scaledSize = NSMakeSize(height * (size.width / size.height),
height);
NSImage *scaledImage = [[[NSImage alloc] initWithSize: scaledSize]
autorelease];
[scaledImage lockFocus];
[image drawInRect: NSMakeRect(0, 0, scaledSize.width,
scaledSize.height) fromRect: NSMakeRect(0, 0, [image size].width,
[image size].height) operation: NSCompositeCopy fraction: 1.0];
[scaledImage unlockFocus];
return scaledImage;
}
- (NSImage *)preview {
[self willAccessValueForKey: @"preview"];
NSImage *previewImage = [self primitiveValueForKey: @"preview"];
[self didAccessValueForKey: @"preview"];
if (previewImage == nil) {
NSData *previewImageData = [self valueForKey: @"picture"];
if (previewImageData != nil) {
previewImage = [NSUnarchiver unarchiveObjectWithData:
previewImageData];
[self setPrimitiveValue: previewImage forKey: @"preview"];
}
}
return previewImage;
}
- (void)setPreview: (NSImage *)value {
if (value == nil)
value = [NSImage imageNamed: @"nopicture.png"];
else
value = [self scaleImage: value toHeight: heightDefault];
[self willChangeValueForKey: @"preview"];
[self setPrimitiveValue: value forKey: @"preview"];
[self didChangeValueForKey: @"preview"];
[self setValue: [NSArchiver archivedDataWithRootObject: value]
forKey: @"picture"];
[self setValue: nil forKey: @"scaledPreview"];
}
- (NSImage *)scaledPreview {
[self willAccessValueForKey: @"scaledPreview"];
NSImage *previewImage = [self primitiveValueForKey: @"scaledPreview"];
[self didAccessValueForKey: @"scaledPreview"];
if (previewImage == nil) {
NSLog(@"1");
previewImage = [self scaleImage: [self valueForKey: @"preview"]
toHeight: scaledDefault];
[self setPrimitiveValue: previewImage forKey: @"scaledPreview"];
}
return previewImage;
}
- (void)setScaledPreview: (NSImage *)value {
NSImage *scaledPreview = [self scaleImage: [self valueForKey:
@"preview"] toHeight: scaledDefault];
[self willChangeValueForKey: @"scaledPreview"];
[self setPrimitiveValue: scaledPreview forKey: @"scaledPreview"];
[self didChangeValueForKey: @"scaledPreview"];
}
The scaledPreview is used in NSPopUpButtons and NSTableViews. My
binding looks like so:
Controller Key: arrangedObject
Model Key Path: preview.scaledPreview
This fails with a "Can't cache image" error. If I use
preview.picture with the NSUnarchiveData transformer, it works fine.
This implementation worked fine when I had the images as part of the
File class. I changed it for performance reasons.
I'm sure there's a way to force loading through the scaledPreview key?
Arved
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden