Programmatically create NSImageView

Hi

I’m taking the plunge in learning Obj-C and it seems I’ve passed the first learning curve of getting over the syntax and mild annoyance at some of the pecularities of the language (arrays and mutable arrays, I’m looking at you) (I come from a heavy PHP/Javascript/AppleScript background).

OK, enough about me and more on you and specifically your immense knowledge of all things XCode. I’ve successfully plunked an image into an NSImageView, did a small victory lap around the office and now want to tackle another baby step…say I would like to create a series of image views based on the index count of an array…how does one programmatically write NSImageViews (or any object for that matter).

I’m searched and, well, obj-c documentation (that doesn’t assume you and Aaron Hillegass or Steve Jobs are roommates) is harder to find than a pound bag of spice on Dune…:smiley: Can someone, in simpleton terms, outline that process or link me to some site/post that can?

Thanks

Hi,

you can create almost any UI element (including NSImageView) with its initWithFrame: method.

Thanks Stefan…

OK, so here is what I’ve done, feel free to critique on any areas of improvements…hopefully this will help someone else just starting out as well…

In IB, I placed a control view in my window.

In the editor I created two classes, appControl and appImage.

appImage was created by selecting New–>File–>Objective-C NSView subclass

appImage.h looks like such:




#import <Cocoa/Cocoa.h>

@interface appImage : NSView {
	
//first two strings are placeholders for some future stuff, the NSImage is what we'll be working with for now
	NSString *thisimageview;
	NSString *imagename;
	NSImage *elvisimage;
}


/*this is the method that'll set/store the data about the image, two vars, thisimageview and imagename
thisimageview is so I can keep track of which image view I am working on, imagename is the file that'll be called in*/

-(void) setImage:(NSString *) thisimageview getImage:(NSString *) imagename; 

@end


appImage.m has this code:



#import "appImage.h"


@implementation appImage

/*XCode will write this for you, I added the code below the initialization code here comment. All this is doing is letting the app know where the image file is located.*/

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
		elvisimage = [NSImage imageNamed:@"fatelvis.png"];
		//[imageView setImage: imageFromBundle];
		
    }
    return self;
}

/*once again XCode will write the shell of this function for you. I added below the drawing code here comment.The first line (NSColor) was a test to make sure I was hitting the control view. NOTE: Once you create this don't forget to go back to IB and change the class of the control view to appImage.

then I got the size of my image and set NSRect to it, placing it at the 0, 0 coordinates. Then I drew the image. */

- (void)drawRect:(NSRect)rect {
    // Drawing code here.
	//[[NSColor blueColor] set];
	NSSize bounds = [elvisimage size];
	NSRect makeitso = NSMakeRect(0, 0, bounds.width, bounds.height);
	
	[elvisimage drawInRect: makeitso fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0]; 

}

//this is going to be for future stuff to keep track of data on the image

-(void) setImage:(NSString *) thisimageview getImage:(NSString *) imagename { 
	NSLog(@"okay");
}

@end