NSImage disappears

Moderator
Posts: 608
Joined: 2002.04
Post: #1
I have a class named ImageFrames that has two methods like this:
Code:
// 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:
Code:
// 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?
Quote this message in a reply
Member
Posts: 104
Joined: 2002.04
Post: #2
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.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #3
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.
Code:
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...
Quote this message in a reply
Member
Posts: 89
Joined: 2002.04
Post: #4
Have you tried converting your array to an NSArray object? I doubt this would make a difference but it's something else to try...
Quote this message in a reply
blb
Unregistered
 
Post: #5
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?
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #6
Ah! That was the problem. I was under the impression that awakeFromNib was called before initWithFrame. Now that I know, it works fine Grin 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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  NSImage leaking? Cirdan 5 3,592 Nov 16, 2009 12:02 PM
Last Post: SethWillits
  Sending an NSImage on a Network mindwalkernine 1 2,998 Aug 9, 2006 04:23 AM
Last Post: djork
  Animate An NSImage mindwalkernine 1 4,007 Jun 18, 2006 12:30 AM
Last Post: StealthyCoin
  NSImage and NSView problems Joseph Duchesne 1 3,681 Aug 19, 2005 07:36 AM
Last Post: unknown
  Faking a Z Coordinate With NSImage Nick 2 2,737 Jun 11, 2005 05:38 PM
Last Post: Nick