我对为什么这段代码不显示任何图像感到困惑:
在应用委托中:
- (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
蓝色图像视图.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
文件 imagefile.png 存在。NSAssert 不会导致异常。NSLog 正在触发。但是没有图像显示在窗口中。