Frage

I'm new to Obj-C/Cocoa programming and facing to a problem about NSImageView.
My goal is to create an app which allow the user to put an image on it with drag&drop.

The problem is the following : when we're dropping an image larger than the window, it resizes the window to the image size, I don't want that !
I put my NSImageView into a NSScrollView so only a certain part of the image MUST be showed and the user will move sliders to view the entire one (or simply resize the window), that's what I want..

Here is my XIB hierarchy (concerned part) :

  • View
    • NSScrollView
      • View
        • NSImageView


In the code of my NSImageView, I have (about that problem) :

[self setImageScaling:NSScaleProportionally];
[self setImage:newImage];


NOTE: The sliders seem to move the view and not the image
NOTE2: I can't resize the window to a lower size than the image

War es hilfreich?

Lösung

I have a horrible aversion to nib files and prefer to do things programmatically whenever possible so I'm not much help with that... but here's a little listing from a little NSScrollview test app I recently coded up that does something similar. The only non-self-contained thing mentioned in this code chunk is the window name, which refers to an NSWindow * for a window that was already created. And self here just refers to a controller object.

NSImage * myImage       = [NSImage imageNamed:@"myfilename.png"];
NSRect imageRect        = NSMakeRect(0.0,0.0,myImage.size.width,myImage.size.height);


self.myImageView        = [[NSImageView alloc] initWithFrame:imageRect];
self.myImageView.bounds = imageRect;
self.myImageView.image  = myImage;

self.myScrollView       = [[NSScrollView alloc] initWithFrame:[window.contentView frame]];
self.myScrollView.hasVerticalScroller = YES;
self.myScrollView.hasHorizontalScroller = YES;
self.myScrollView.documentView = self.myImageView;
self.myScrollView.borderType = NSNoBorder;
self.myScrollView.scrollerStyle = NSScrollerStyleOverlay;

window.contentView      = self.myScrollView;

I know that's perhaps not the solution you were looking for but maybe it helps you debug? In particular, I didn't do anything funny with my NSImageView besides give it a frame, a bounds rectangle, and an NSImage *, so I don't think you need to worry about the scaling parameter per se -- in fact, if you want the image to be full-sized and to scroll around it, you want your NSImageView's frame to be the same size as the image so that it doesn't HAVE to scale at all. Maybe that's your problem -- the frame is too small? It is OK for the NSImageView frame to be bigger than the window -- that's what lets you scroll around it.

Andere Tipps

To me it sounds like your views don't have the proper sizes. Because of bouncing and because your views don't clip their subviews, you can still scroll a bit, but it returns to its original position.

  1. Check if the NSImageView is big enough to display your full image. (Check frame or bounds for width and height)

    If it is not big enough do this (inside the method, where you set the new image):

    self.myImageView.frame = NSMakeRect(0.0,0.0, self.myImageView.image.size.width, self.myImageView.image.size.height);

  2. Adjust the size of your scroll views view accordingly.

    self.myScrollView.view.frame = self.myImageView.frame;

Now your scrollable area should be big enough to cover the full image.

If this works for you, give Matt some credit as well, by up-voting his answer, since his answer does essentially the same thing - only code based, instead of initialising everything using a nib file - and the code I used is very similar to what he had already posted.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top