Cocoa--获得一个简单的NSImageView来工作
我不明白为什么此代码不显示任何图像: 在应用程序代理中:Cocoa--获得一个简单的NSImageView来工作,cocoa,nsimageview,Cocoa,Nsimageview,我不明白为什么此代码不显示任何图像: 在应用程序代理中: - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSRect rect = window.frame; rect.origin.x = 0; rect.origin.y = 0; BlueImageView *blueImageView = [[BlueImageView alloc]initWithFrame:
- (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
文件imagefile.png已存在。NSAssert未导致异常。NSLog正在发射。但是没有图像显示在窗口中。调用drawRect:方法来绘制视图,您的实现立即返回。要让NSImageView为您绘制图像,请调用
[super-drawRect:dirtyRect]代码>在drawRect的实现中:。如果您不打算在drawRect:中进行任何其他绘图,只需删除该方法即可加快绘图速度。将调用drawRect:方法来绘制视图,您的实现将立即返回。要让NSImageView为您绘制图像,请调用[super-drawRect:dirtyRect]代码>在drawRect的实现中:。如果不打算在drawRect:中进行任何其他绘图,只需删除该方法即可加快绘图速度