Cocoa -- getting a simple NSImageView to work
- by William Jockusch
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.