我想在我的主视图中显示 NSImageView
:
img=[[NSImageView alloc]init];
[img setImage:[[NSImage alloc]initWithContentsOfFile:filename]];
width=img.image.size.width/2;
height=img.image.size.height/2;
[img setFrame:NSMakeRect(0,0,width ,height)];
mainview = [((AppDelegate *)[NSApp delegate]).window contentView];
[mainview addSubview:img];
图像正确显示,但它不是位于主窗口的左上角,而是完全位于屏幕的右侧。
上面有什么问题吗?
最佳答案
尝试这样:-
- (void)windowDidLoad
{
NSString *imageName = [[NSBundle mainBundle] pathForResource:@"yourImage ofType:@"tiff"];
NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:imageName];
[yourimgView setImage:photoImage];
CGFloat width=yourimgView.bounds.size.width;
CGFloat height=yourimgView.bounds.size.height;
CGFloat xboundspoint=yourimgView.bounds.origin.x;
CGFloat yboundspoint=yourimgView.bounds.origin.y;
[yourimgView setFrame:NSMakeRect(xboundspoint, yboundspoint+150.0, width, height)];
[yourview addSubview:yourimgView];
[[[self window]contentView]addSubview:yourview];
[super windowDidLoad];
}
关于macos - 无法在 NSView 中正确定位 NSImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18881960/