0
NSWindow
  NSImageWell - with constraint to all four side of the Window's containerView
    NSImage - is put in Well via NSPageControl

Using the mouse, I resize the Window and the ImageWell expands or contracts with the Window as desire.

The image in the Well remains the same size regardless of Window size.

The image does resize correctly, based on the size change of Window, if I drag the image a bit with the mouse.

How and where do I send a message to get this size to change "automatically" ?

Do I need to monitor size changes in the Window using Notifications?

Sorry, I was not clear. I want the image in the image well to maintain its aspect ratio and to resize itself each time the Window is resized. Currently, when the window is resized, the user sees no change in the size of the image. He can however, cause the image to resize correctly by click dragging on the image or Window content view. I want the user to see this resizing take place immediately, without needing to use the mouse. For example of an image that has an aspect ratio of 4/3. if the window is 4" x 4" , then when the image is added, it appears as 4" by 3". If the window goes to 8" x 8", then the image should automatically go to 8" x 6".

Guilherme was good enough to send me his demo NSView app. If, like in his app, I set an image in IB, my app also successfully resizes not only the ImageView but also the Image itself.

The problem appears to be because I put the images into the ImageView via an NSPageController. The only way to get the image to resize is to do a two finger drag inside the window.contentView

After Resizing window:

<a href="https://www.dropbox.com/s/9sywqf1kpgl33yb/Screen%20Shot%202015-09-12%20at%2013.26.03.png?dl=0">New Image Appears</a>

Then following a two-finger drag on image :

<https://www.dropbox.com/s/d2he6bdzxbeefok/Screen%20Shot%202015-09-12%20at%2013.26.50.png?dl=0>

Below is relevant code:

AppDelegate follows:

#import "AppDelegate.h"
#import "MyImageView.h"

@interface AppDelegate ()
@property (strong, nonatomic) IBOutlet NSWindow *window;
@property (strong, nonatomic) IBOutlet NSPageController *pageController;
@property (strong, nonatomic) IBOutlet MyImageView *imageView;
@property (weak) IBOutlet NSTextField *infoLabel;
@property NSArray *imageArray;
@end

@implementation AppDelegate

#pragma mark Window delegate

- (void)windowDidResize:(NSNotification *)notification{
    //[_window layoutIfNeeded];
    //[_imageView doSelfLayout];
}

- (void)awakeFromNib {
    [_window setDelegate:self];
    [_imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    _imageArray = @[ [NSImage imageNamed:@"eggplant.png"],
                     [NSImage imageNamed:@"sandwich.png"],
                     [NSImage imageNamed:@"yoghurt.png"]];

    [_pageController setDelegate:self];
    [_pageController setArrangedObjects:_imageArray];
    [_pageController setTransitionStyle:NSPageControllerTransitionStyleStackBook];

    // Set info label's text
    NSString *info = [NSString stringWithFormat:@"Image %ld/%ld", ([_pageController selectedIndex]+1), [_imageArray count]];
    [_infoLabel setStringValue:info];
}

#pragma mark Page Controller delegate methods:

- (void)pageController:(NSPageController *)pageController didTransitionToObject:(id)object {
    /* When image is changed, update info label's text */
    NSString *info = [NSString stringWithFormat:@"Image %ld/%ld", ([_pageController selectedIndex]+1), [_imageArray count]];
    [_infoLabel setStringValue:info];
    //[_window layoutIfNeeded];
}

- (NSString *)pageController:(NSPageController *)pageController identifierForObject:(id)object {
    NSString *identifier = [[NSNumber numberWithInteger:[_imageArray indexOfObject:object]] stringValue];
    return identifier;
}

- (NSViewController *)pageController:(NSPageController *)pageController viewControllerForIdentifier:(NSString *)identifier {
    /* Create new view controller and image view */
    NSViewController *vController = [NSViewController new];
    NSImageView *iView = [[NSImageView alloc] initWithFrame:[_imageView frame]];
    [iView setImage:(NSImage *)[_imageArray objectAtIndex:[identifier integerValue]]];
    [iView setImageFrameStyle:NSImageFrameNone];

    /* Add image view to view controller and return view controller */
    [vController setView:iView];
    return vController;
}
@end
mbarron
  • 99
  • 2
  • 9
  • It's not clear what you want. Do you want to be notified when the frame changes or do you want to do something else (you wrote the everything works) – Marek H Sep 11 '15 at 20:27

2 Answers2

0

What option have you selected for the scaling property in IB?

Scaling image proportionally up or down

Here's a sample project, the image resizes automatically with Proportionally Up or Down selected.

Guilherme Rambo
  • 2,018
  • 17
  • 19
  • Guilherme, Thanks for your input! My ImageWell is being resized correctly, it is the image inside the Well that is not initially appearing at the correct size following a Window resizing. – mbarron Sep 12 '15 at 00:07
  • I am using Proportional Up and Down scaling. – mbarron Sep 12 '15 at 00:08
0

I have made a demo Cocoa project zipfile below that has what I was looking for, namely an OS X app that :

- uses NSPageController in History mode to display an array of images (Portrait and Landscape) in a resizeable window
- imageView maintains imagefile aspect ratio as it automatically resizes
- images list can be traversed using a PreviousButton and a NextButton, or using a touch pad, with a two-finger swipe.

https://www.dropbox.com/s/7qgmg5dr1bxosqq/PageController3%203.zip?dl=0

It is pretty basic stuff, but I post it with the hope it can same someone else some time. Mark

mbarron
  • 99
  • 2
  • 9
  • Your dropbox link is no longer working. I'm having this exact same problem and would really like to see the code to fix the image resizing problem. – Mark Lilback Feb 03 '16 at 20:52