1

I have an NSImageView that encloses a dynamically generated NSImage. When I change the image displayed, I would like to dynamically resize the NSImageView so that it precisely wraps the new image, and also have the enclosing window resize so that the space between the NSImageView and every other part of the window remains constant. (Note that the image view's scaling is set to none, as I want its image to always be shown at its physical size.) To illustrate, suppose I begin with a small image in my image view. If I replace it with a large image, I wish for both the NSImageView and enclosing window to resize to accommodate it, without affecting the sizing or spacing of any other element.

Currently, I call the following method whenever the magnification level is changed via the stepper or associated text field. Though regenerating the image and loading it into the NSImageView works fine, resizing the NSImageView and enclosing window do not.

- (void) updateMagnification:(NSUInteger)newMagnification {
    // Keep values of stepper and associated text field synchronized.
    [self.magnificationStepper setIntegerValue:newMagnification];
    [self.magnificationTextField setIntegerValue:newMagnification];

    // Regenerate image based on newMagnification and display in image view.
    [self.qrGenerator generateWithBlockPixelWidth:newMagnification];
    self.imageView.image = self.qrGenerator.image;

    // Adjust frame size of image view.
    NSLog(@"Old size: frame=%@ image=%@", NSStringFromSize(self.imageView.frame.size), NSStringFromSize(self.imageView.image.size));
    [self.imageView setFrameSize:NSMakeSize(self.imageView.image.size.width, self.imageView.image.size.height   )];
    NSLog(@"New size: frame=%@ image=%@", NSStringFromSize(self.imageView.frame.size), NSStringFromSize(self.imageView.frame.size));

    //[self.window setViewsNeedDisplay:YES];
    //[self.imageView setNeedsDisplay:YES];
    //[self.imageView.superview setNeedsDisplay:YES];
}

Regardless of whether I increase or decrease the magnification value, causing the image to grow smaller or larger, the size of both the NSImageView and window remains constant. The three setNeedsDisplay: calls that are commented out have no effect even if they're uncommented -- they were my attempt to determine if the problem was related to the controls not redrawing once their size was adjusted, but the calls had no effect. Curiously, my NSLog calls indicate that the imageView's frame does indeed take the requested size, for they yield this output:

2012-06-12 11:02:50.651 Presenter[4660:603] Old size: frame={422, 351} image={168, 168}
2012-06-12 11:02:50.651 Presenter[4660:603] New size: frame={168, 168} image={168, 168}

The actual display, of course, does not change.

Interestingly, changing the imageView's frame style to "none," either in Interface Builder or programatically with [self.imageView setImageFrameStyle:NSImageFrameNone], gives me behaviour closer to what I desire. Making the image larger so that it would otherwise be clipped by the image view does indeed result in the image view and window growing larger. From this point, however, making the image smaller does not result in the image view or window resizing accordingly. "None" is the only image frame style that displays this somewhat correct behaviour -- all four of the bordered styles (i.e., bevel [which is the default], button, groove, and photo) show the same entirely incorrect behaviour described above.

I came across someone with a similar problem. Oddly, he only observed the problematic behaviour when his image view's frame style was set to NSImageFrameNone, when this is the only value that gives me somewhat-correct behaviour. I tried modifying the frame style to a non-none value before the resize and to none afterward, as this resolved the other person's problem, but for me, this yielded the same behaviour as when I simply set the frame style to "none" initially.

Any help you provide will be much appreciated. Thanks!

Community
  • 1
  • 1

0 Answers0