Edit AllPages


An NSImageView displays a single NSImage in a frame and can optionally allow a user to drag an image to it.

To display an image from the application bundle using an NSImageView use the following code:

// Header IBOutlet *NSImageView imageView;

// Source NSImage *imageFromBundle = [NSImage imageNamed:@”your_image.tif”];

[imageView setImage: imageFromBundle];

To download and display an image from a server using an NSImageView use the following code:

// Header IBOutlet *NSImageView imageView;

// Source NSURL *imageURL = [NSURL URLWithString:@””]; NSData *imageData = [imageURL resourceDataUsingCache:NO]; NSImage *imageFromBundle = [[NSImage alloc] initWithData:imageData];

if (imageFromBundle) { // The image loaded properly, so lets display it. [imageView setImage:imageFromBundle]; [imageFromBundle release]; } else // if (!imageFromBundle) { // The image did not load properly, so lets send an error to the log. // At this time, we could either load a copy of the image from the bundle, // or simply display an error sheet. NSLog(@”imageView could not be loaded.”); }

Other failure modes: URL, Data?

The code is just meant to be a simple examples showing how to do a few things with NSImageView.


Despite my efforts the following statement refuses to live up to itself:

It shrinks to fit inside the frame, but it does NOT expand to fill the frame.

This is the intended, if poorly documented, behavior of NSImageView. It’s worked that way in OpenStep as well. Feelings of mistrust or betrayal are natural.

I ended up writing my own class, which I call FakeImageView, to implement this and a few other features I wanted. Click through for more details.

When necessary, I create a FakeImageView subclass of NSView in IB and set image views to that custom class. Maybe it would have been better to subclass NSImageView … but oh well. Your mileage may vary.

– MikeTrent

I liked the Drag and Drop support of NSImageView – I’m going to try to add that functionality to your FakeImageView.

Is there a way to repeat an image over the X axis, forming a pattern?

Sure. If I was doing it, I’d add that to NSImageCategory though. +patternImage: alongAxis: size:, maybe.

See NSColor’s colorWithPatternImage: method.

I needed an image view to use in a CoreData project that would let me get and set the path of the displayed image (because I wanted to use discrete image files instead of ballooning the CD store). Here’s what I came up with.

//// Header #import <Cocoa/Cocoa.h>

@interface PRImageView : NSImageView { AliasHandle mImageAlias; BOOL mDraggingFlag; }


//// Implementation // // PRImageView.m // // Created by Gregory Weston on 3/5/08. //

#import “PRImageView.h”

@implementation PRImageView