Skip navigation.
 
mlRe: How to show CGImageRef over an overlay nswindow.
FROM : Alastair Houghton
DATE : Thu Nov 29 19:16:22 2007

On 29 Nov 2007, at 12:29, Amit8 J wrote:

> Hello all,
> I am stuck to a problem related to core graphics. Actually i want to
> display an image over a overlay nswindow. For creating an cgimageref i
> have written the following code:


[snip]

You're going about this the hard way.  What you need to do is:

1. Create your NSWindow.

2. Add an NSImageView to the new window (e.g. by doing -
setContentView:).  You'll want to configure it as well (e.g. set the 
frame style, image alignment and image scaling properties).

3. Use NSImage to load the file (no need to use CGImage; that's just 
complicating matters).  You should be able to do [NSImage 
imageNamed:@"originalColor.png"] to do this.  (Compare with the code I 
chopped out.)

4. Send your NSImageView a -setImage: message to change its image.

Also, I don't think you need to subclass the window.  Generally when 
you're writing Cocoa programs, you only subclass when you absolutely 
have to; this is quite a contrast to many other OO frameworks 
(particularly the Windows-based ones) where you tend to end up 
subclassing things all the time.

Anyway, most of the code you put into your NSWindow subclass could go 
into a method that creates and sets up the window and the image view.

The other thing that you perhaps didn't understand is that in Cocoa, 
you do your drawing in an NSView subclass's -drawRect:.  Normally, 
anyway.  So if you wanted to do the actual drawing yourself, you'd 
need to write a view that draws an image in its -drawRect:.  Since 
this is a very common requirement, unsurprisingly there is already 
such a view (NSImageView), so there's no need to bother in this 
case... you can just use the ready-built one.

Kind regards,

Alastair.

--
http://alastairs-place.net

Related mailsAuthorDate
mlHow to show CGImageRef over an overlay nswindow. Amit8 J Nov 29, 13:29
mlRe: How to show CGImageRef over an overlay nswindow. Alastair Houghton Nov 29, 19:16