Re: Drawing an image in a CustomView
Re: Drawing an image in a CustomView
- Subject: Re: Drawing an image in a CustomView
- From: "Erik M. Buck" <email@hidden>
- Date: Mon, 14 Jan 2002 22:40:42 -0600
- Organization: EMB & Assocites Inc.
I humbly request that you do not use this list for this type of question any
more. If you don't know why not, take that as the reason. When you have a
non-trivial question that is not easily answered by the resources at your
disposal, I am sure we will all enjoy working on it with you.
Here is one answer (I have not compiled it):
#import <AppKit/AppKit.h>
@interface SimpleImageView : NSView {
NSImage *image;
}
@end
/* simpleImageView.m */
#import "SimpleImageView.h"
@implementation SimpleImageView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
image = [[NSImage alloc] initWithContentsOfFile:@"macosxlogo.gif"];
}
return self;
}
- (void)drawRect:(NSRect)rect {
// Drawing code here.
[image compositeToPoint:[self bounds].origin
operation:NSCompositeSourceOver];
}
@end
1) You might want to erase the background before drawing an image.
2) You might want to use an accessor to set the image, but that is left as
an exercise. You made a good start with your -setImage: method.
3) Note the capitalization conventions.
4) In your original code you were not drawing anything.
5) You were redundantly initializing the image.
6) The :@"macosxlogo.gif" file MUST be available (probably in the current
directory) for this to work.
See NSBundle for ways to get the path to an image resource regardless of the
current working directory.
7) This example is painfully obvious in the book you cites and others.
There is an example on your hard disk that does exactly this.
8) Consult the class documentation for classes that you use. Always look at
the superclass documentation for classes that you use.
9) You got the error that you did because you were sending a message to a
Class object that does not understand the message. You wanted to use self
as the receiver of the message, but you had bigger problems.
The error was further masked by you failure to use the capitalization conven
tions.
----- Original Message -----