Re: How to draw a NSImage in a view
Re: How to draw a NSImage in a view
- Subject: Re: How to draw a NSImage in a view
- From: Kyle Mandli <email@hidden>
- Date: Tue, 10 Jul 2001 10:37:56 -0500
>
I have a subclass of NSView with two methods :
>
- (void)drawRect:(NSRect)rect
>
{
>
NSRect myBounds = [self bounds];
>
>
[tableViewImage drawInRect:myBounds
>
fromRect:myBounds
>
operation:NSCompositeCopy
>
fraction:nil];
>
}
>
The fromRect is a rectangle based in the NSImage coordinate system. If
you want the entire image to appear use NSMakeRect(0,0,[myImage
size].width,[myImage size].height). The fraction argument specifies the
transparency of the image between 0 and 1. Kind of a neat affect.
'1.0' is opaque.
>
- (void)setTableViewImage:(NSImage *)image
>
{
>
[tableViewImage autorelease];
>
tableViewImage = [image copy];
>
[self setNeedsDisplay:YES];
>
}
First of all you may not want to copy the image. If you change it
somehow and still want an original representation you might want to
cache it off screen. Otherwise just retain the object. As for the
reason it doesn't display anything, that probably has to do with your
drawRect method above. Try it and if it still doesn't work say
something.
On another note, most of this stuff you were asking can be found in the
NSImage class API on the web or through project builder up in the help
menu. Or if that doesn't help just look in the header files included in
each project (in frameworks). Also another good thing to look at is the
stuff on retain, copy, autorelease and release is at stepwise:
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
Good luck!
Kyle