PDA

View Full Version : NSImage disappears


Josh
2002.10.31, 01:26 PM
I have a class named ImageFrames that has two methods like this:

// In interface
NSImage *imageCache[2];

// in implementation
- (void)loadImage:(NSString *)path forFrame:(int)frame
{
imageCache[frame] = [[NSImage imageNamed:path] retain];
[imageCache[frame] setDataRetained:YES];
[imageCache[frame] lockFocus];
[imageCache[frame] unlockFocus];

if(imageCache[frame] == NULL)
{
NSLog(@"loadImage: image is null");
}
}

- (NSImage *)imageAtFrame:(int)frame
{
if(imageCache[frame] == NULL)
{
NSLog(@"frame num %d is null", frame);
}

return(imageCache[frame]);
}
And then I use it like so:

// in interface
ImageFrames *test;

// in awakeFromNib
test = [[ImageFrames alloc] init];

// in initWithFrame
[test loadImage:@"testImage" forFrame:1];

// in drawRect
[[test imageAtFrame:1] compositeToPoint:NSMakePoint(10, 10) operation:NSCompositeSourceOver];

I always get the message from -imageAtFrame that the image is null. I have tried using initWithContentsOfFile instead of imageNamed but it still doesn't work. Why?

GoodDoug
2002.10.31, 07:15 PM
Make sure the image is getting copied to your Resources folder during the build. Have you tried stepping through this with the debugger? That might shed some light on the issue.

Josh
2002.11.01, 09:57 AM
Yes, the image is in the appropriate folder and in the bundle. When I go through with the debugger, the image is there, but then, when it leaves the -loadImage:forFrame: it disappears. As if it hadn't been retained. I tried calling -retain on it twice, but still nothing. If I put this in the -init method, then isa is not null, but everything else still is.

frameCache[0] = [[NSImage alloc] init];
frameCache[1] = [[NSImage alloc] init];
This is driving me crazy, in case you couldn't tell. In theory, this should work...

flipflop
2002.11.01, 12:17 PM
Have you tried converting your array to an NSArray object? I doubt this would make a difference but it's something else to try...

blb
2002.11.01, 04:14 PM
Let me see if I understand the whole of your code: in awakeFromNib, you alloc/init the instance, but in initWithFrame: you load the image into the instance, which won't be created until awakeFromNib?

Josh
2002.11.01, 07:41 PM
Ah! That was the problem. I was under the impression that awakeFromNib was called before initWithFrame. Now that I know, it works fine :D jnikolai, I actually starting with an NSArray but when I had trouble I thought that might be the source of it, so it got axed.