Load an image at startup


I am trying to load a background image at startup, so I created custom view as a sub-class of NSView. I copied the image file into the project. But, the image does not load. I looked at several posts but this got me confused since they try to accomplish different results.

I went back and read Shane’s chapters on Dealing with Windows and Images, but they deal with different topics as well.

Your help is appreciated.


Here is my code:

script myView
property parent : class “NSView”

on initWithFrame_(frame)
	continue initWithFrame_(frame)
	set theRect to {{15, 15}, {450, 450}}
	set imageView to current application's NSImageView's alloc()'s initWithFrame_(theRect)
	imageView's setImageScaling_(current application's NSScateToFit)
	set theImage to current application's NSImage's imageNamed_("RedCircle.png")
	imageView's setImage_(theImage)
	return me
end initWithFrame_

on drawRect_(dirtyRect)
	-- Drawing cofde here.
end drawRect_

end script

Why not just use an image view?

HI Shane - Yes, It will give me the results, but I want to work on this type of coding as I seem to struggle with it. Practicing a harder code is the only way for me to improve.


Well that code looks odd. You’re not calling addSubview_ on the view, so that’s not going to work. Try “tell me to addSubView_…”. “NSScateToFit” looks like a typo. And you’re overriding drawRect_ and having it do nothing, which means nothing will get drawn – remove that.

It worked. Thank you so much Shane.