Cocoa -- getting a simple NSImageView to work

Posted by William Jockusch on Stack Overflow See other posts from Stack Overflow or by William Jockusch
Published on 2011-01-09T02:34:27Z Indexed on 2011/01/09 2:54 UTC
Read the original article Hit count: 292

Filed under:
|

I am confused about why this code does not display any image:

In the app delegate:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSRect rect = window.frame;
    rect.origin.x = 0;
    rect.origin.y = 0;
    BlueImageView *blueImageView = [[BlueImageView alloc]initWithFrame:rect];
    window.contentView = blueImageView; // also tried [window.contentView addSubview: blueImageView];
}

BlueImageView.h:

@interface BlueImageView : NSImageView {
}
@end

BlueImageView.m:

@implementation BlueImageView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setImage: [NSImage imageNamed:@"imagefile.png"]];
        NSAssert(self.image, @"");
        NSLog (@"Initialized");
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
}

@end  

The file imagefile.png exists. The NSAssert is not causing an exception. The NSLog is firing. But no image shows up in the window.

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about nsimageview