gpt4 book ai didi

cocoa - 在 View 中绘图 : Why can't I get my NSImage to display in my NSImageView?

转载 作者:行者123 更新时间:2023-12-03 17:49:57 25 4
gpt4 key购买 nike

我使用代码创建了一个 NSImageView:

@implementation IconView

-(void)awakeFromNib {

[self setImageRect:NSMakeRect(36, 66, 144, 144)];
[self setImageView:[[NSImageView alloc] initWithFrame:[self imageRect]]];
[self addSubview:[self imageView]];

//For the frame around the image:
[self setFrameRect:NSMakeRect(18, 42, 180, 180)];
[NSBezierPath setDefaultLineWidth:12];


}

- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];

// Drawing code here.

[[self alertColor] set];
[NSBezierPath strokeRect:[self frameRect]];
[[[self imageView] image] setSize:[self imageRect].size ];
}

我像这样设置 NSImageView 的图像:

@implementation IconViewController


-(void)setColor:(NSColor *)color forApplication:(NSRunningApplication *)app {

IconView* iconView = (IconView*)[self view]; //The view associated with this controller
[iconView setAlertColor:color];
[[iconView imageView] setImage:[app icon]];
NSLog(@"app icon: %@", [app icon]);

[[self view] setNeedsDisplay:YES];

}

日志输出是这样的:

2015-05-07 22:29:27.711 HelloApplication[1569:17913] app icon: <NSImage 0x608000260c00 Size={32, 32} Reps=(
"<NSIconRefImageRep:0x608000083bb0 iconRef=0x103 size:128x128 pixels:128x128>",
"<NSIconRefImageRep:0x608000083d90 iconRef=0x103 size:128x128 pixels:256x256>",
"<NSIconRefImageRep:0x608000083e30 iconRef=0x103 size:256x256 pixels:256x256>",
"<NSIconRefImageRep:0x608000083de0 iconRef=0x103 size:256x256 pixels:512x512>",
"<NSIconRefImageRep:0x608000084150 iconRef=0x103 size:512x512 pixels:512x512>",
"<NSIconRefImageRep:0x608000084100 iconRef=0x103 size:48x48 pixels:48x48>",
"<NSIconRefImageRep:0x608000084790 iconRef=0x103 size:36x36 pixels:36x36>",
"<NSIconRefImageRep:0x608000084ec0 iconRef=0x103 size:36x36 pixels:72x72>",
"<NSIconRefImageRep:0x608000084470 iconRef=0x103 size:32x32 pixels:32x32>",
"<NSIconRefImageRep:0x608000085000 iconRef=0x103 size:32x32 pixels:64x64>",
"<NSIconRefImageRep:0x608000085050 iconRef=0x103 size:18x18 pixels:18x18>",
"<NSIconRefImageRep:0x608000084fb0 iconRef=0x103 size:18x18 pixels:36x36>",
"<NSIconRefImageRep:0x6080000850a0 iconRef=0x103 size:16x16 pixels:16x16>",
"<NSIconRefImageRep:0x6080000850f0 iconRef=0x103 size:16x16 pixels:32x32>",
"<NSIconRefImageRep:0x608000085140 iconRef=0x103 size:512x512 pixels:1024x1024>"
)>

所以,看起来我有一个 NSImage——但它不会显示。我看到框架矩形的笔画颜色正确,但没有图像显示。 IB 显示在 IconView.xib 中我的 IconViewController(文件所有者)已连接到 IconView:

enter image description here

我的窗口 Controller 执行此操作:

//
// MyWindowController.m
// HelloApplication
//

@implementation MyWindowController


- (void)windowDidLoad {
[super windowDidLoad];

// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.

}

- (void)setUpView {

[self setViewCtrl:[[IconViewController alloc] initWithNibName:@"IconView" bundle:nil]];


NSView* view = [[self viewCtrl] view];
[[self window] setContentSize:[view bounds].size]; //[view bounds] returns a struct, whose elements must be
[[self window] setContentView:view]; //accessed with dot notation.


}

-(id) initWithWindowNibName:(NSString *)windowNibName {

if (self = [super initWithWindowNibName:windowNibName]) {
[self setUpView];
}

return self;
}

@end

我的 AppDelegate 执行此操作:

//
// AppDelegate.m
// HelloApplication
//

#import "AppDelegate.h"
#import "MyWindowController.h"


@interface AppDelegate ()

@property(strong) MyWindowController* windowCtrl;

@end


@implementation AppDelegate


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

[self setWindowCtrl:[[MyWindowController alloc] initWithWindowNibName:@"MainWindow"]];
[[self windowCtrl] showWindow:self];

}

如果您需要更多文件或屏幕截图,请告诉我:

//
// IconView.h
// HelloApplication
//

#import <Cocoa/Cocoa.h>

@interface IconView : NSView

@property(copy) NSColor* alertColor;
@property(weak) NSImageView* imageView;

@end

...

//
// IconView.m
// HelloApplication
//

#import "IconView.h"

@interface IconView()

@property(assign) NSRect frameRect;
@property(assign) NSRect imageRect;


-(void)awakeFromNib;

@end


@implementation IconView

-(void)awakeFromNib {

[self setImageRect:NSMakeRect(36, 66, 144, 144)];
[self setImageView:[[NSImageView alloc] initWithFrame:[self imageRect]]];
[self addSubview:[self imageView]];

[self setFrameRect:NSMakeRect(18, 42, 180, 180)];
[NSBezierPath setDefaultLineWidth:12];


}

- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];

// Drawing code here.

[[self alertColor] set];
[NSBezierPath strokeRect:[self frameRect]];
[[[self imageView] image] setSize:[self imageRect].size ];
}

@end

...

//
// IconViewController.h
// HelloApplication
//

#import <Cocoa/Cocoa.h>
#import "MyActivityMonitorDelegateProtocol.h"

@interface IconViewController : NSViewController <MyActivityMonitorDelegateProtocol>


@end

...

//
// IconViewController.m
// HelloApplication
//

#import "IconViewController.h"
#import "IconView.h"

@interface IconViewController ()

-(void)setColor:(NSColor*)color forApplication:(NSRunningApplication*)app;

@end



@implementation IconViewController


-(void)setColor:(NSColor *)color forApplication:(NSRunningApplication *)app {

IconView* iconView = (IconView*)[self view]; //The view associated with this controller
[iconView setAlertColor:color];
[[iconView imageView] setImage:[app icon]];
NSLog(@"app icon: %@", [app icon]);

[[self view] setNeedsDisplay:YES];

}

-(void)applicationDidLaunch:(NSRunningApplication *)app {
[self setColor:[NSColor greenColor] forApplication:app];
}

-(void)applicationDidTerminate:(NSRunningApplication *)app {
[self setColor:[NSColor redColor] forApplication:app];
}


- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
}

@end

这是一本旧 cocoa 书的示例,第 240-241 页:

Cocoa Programming: A Quick Start Guide for Developers.

回复@Joshua Nozzi的回答:

(1)

-drawRect:— Don't set properties here, least of all, something that causes further drawing in subviews (such as setting your image view's image).

我实际上只是设置 ImageView 图像的大小。我遵循的示例尽职尽责地从 drawRect: 中删除了只需要调用一次的任何代码,但是当我尝试在其他地方设置 NSImageView 图像的大小时,例如在 IconViewController 的 viewDidLoad: 方法中,它没有成功设置显示图像的大小,所以它一定发生得太晚了。啊,如果我在告诉 View 需要重绘之前在 IconViewController 的 setColor:forApp: 方法中设置大小,就可以了:

-(void)setColor:(NSColor *)color forApplication:(NSRunningApplication *)app {

IconView* iconView = (IconView*)[self view]; //The view associated with this controller
NSImageView* imageView = [iconView imageView];

[iconView setAlertColor:color];
[imageView setImage:[app icon] ];
//NSLog(@"app icon: %@", [app icon]);
[ [imageView image] setSize:[iconView imageRect].size ];
[[self view] setNeedsDisplay:YES];

}

(2)

Your call to -setupView comes during your window controller's initialization, but your [window's] nib hasn't been loaded yet...

看起来这些是您关心的内容:

- (void)setUpView {


[[self window] setContentSize:[view bounds].size]; //[view bounds] returns a struct, whose elements must be
[[self window] setContentView:view]; //accessed with dot notation.

在窗口取消归档之前调用[[self window] doSomething]意味着[self window]将为nil,并且调用[nil doSomething] 不会做任何事情。我将这些行移至 Window Controller 的 windowDidLoad 方法中:

@implementation MyWindowController


- (void)windowDidLoad {
[super windowDidLoad];

// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.

NSView* view = [[self viewCtrl] view];
[[self window] setContentSize:[view bounds].size]; //[view bounds] returns a struct, whose elements must be
[[self window] setContentView:view]; //accessed with dot notation.


}

似乎还可能存在其他错误:我读到您不应该在 init 方法中调用访问器方法 - 相反,您应该直接访问 ivars。

(3)

You appear to have application delegate methods (did launch / did terminate) in your icon view controller

不,我不认为是这样;我接下来的教程使用了那些令人困惑的方法名称。我自己经常认为它们是 Cocoa 方法,以至于我实际上在代码中添加了一条注释,表明它们是自定义方法。这是 IconViewController 的声明:

@interface IconViewController : NSViewController <MyActivityMonitorDelegateProtocol>

applicationDidLaunch:/applicationDidTerminate:MyActivityMonitorDelegateProtocol声明的方法。方法名称中的应用程序实际上指的是我的应用程序运行时在我的系统上启动/终止的任何应用程序。

最佳答案

几点:

  1. 您的-drawRect:实现应该尽可能快速和轻量级,这意味着它应该只处理绘图。不要在这里设置属性,尤其是那些会导致在 subview 中进一步绘制的属性(例如设置 ImageView 的图像)。

  2. 您对 -setupView 的调用是在窗口 Controller 初始化期间进行的,但您的 Nib 尚未加载。同时,您的 -windowDidLoadNib 方法为空,但其注释建议它用于在从 nib 加载内容后进行设置。这是发送消息到 nil 以及某些/所有事情无法正常工作的方法。尝试从 -windowDidLoadNib 而不是 init 例程设置您的内容。

  3. 您的图标 View Controller 中似乎有应用程序委托(delegate)方法(是否启动/是否终止),但您指定了专用的应用程序委托(delegate)。应用程序委托(delegate)方法需要位于一个位置:实际委托(delegate)。

我希望这会有所帮助。

关于cocoa - 在 View 中绘图 : Why can't I get my NSImage to display in my NSImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30115900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com