Unable to dynamically resize NSImageView - cocoa

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!

Related

NSImageView rending a blurry NSimage despite it being of the same size

I have an NSCollectionViewItem with an NSImageView (32x32) to which i supply a #1x image of the same size.
It looks perfect in the interface builder, but when the app is built, the resolution looks quite off. Is there any particular reason for this?
Just to add that the Image in the asset manager also has a #2x
EDIT: Still investigating this issue, but I have just noticed that If the collection view which contains the collection item, which contains the NSImageView is enclosed by a bordered NSSCrollView the images are perfect (.ie non blurry)
Turns out if you draw images in frames with either the x,y coords or the height and width having fractions you end up with blurry images. passing the drawingRect through NSIntegralRect fixes that.

how to make a bitmap to fit to the static picture control of a property sheet?

I am having a property sheet and added three pages.In the first page I added a picture control and in the properties of that picture control for "Image" property I am setting a bitmap.And was able to load very successfully.But,the problem here I faced when I run my exe then I am getting bitmap on the page header(which is some kind of banner for the page.)But what I noticed is there is some gap between the banner bitmap and the frame edge (which is occurring at the right top corner). And when I ran my xxx.exe on a Japanese machine then I had observed like the sheet is somewhat stretched and banner bitmap is not stretched completely till the edge of the sheet (top right corner)for Instance if we take a dialog on load a bitmap on it then we can observe that it is entirely got stretched till the edge.
So how can we avoid this issue like whatever the operating system it is and whatever the resolution it might be that banner should get stretched till the edge of the sheet.The gap has to be removed.
I am not loading the bitmap dynamically (setting in the properties.)
Can anyone please help me ti achieve this
The reason you get different results on different systems is that dialogs and controls are sized using "dialog units" which are based on the average size of the font, rather than an absolute number of pixels.
By default a static image control will resize itself to the size of the bitmap it's displaying. So if the dialog itself ends up bigger than normal (because the font is physically bigger), the picture control will appear to have shrunk, leaving a gap.
You could try setting the SS_REALSIZECONTROL style on the static control. The normal behavior of the static control (when displaying a picture) is to resize itself to the size of the bitmap, however the SS_REALSIZECONTROL style overrides that behavior and instead causes the bitmap to be resized to the size of the control. However the results of that may be less than optimal (e.g. the aspect ratio will probably be wrong), so instead you may want to look into scaling the bitmap yourself.

How to resize between 3.5" and 4" screens (dynamic constraints in autolayout?)

I have one view filling the screen with a background image. Other views (text fields) are in exact positions (the background image includes the text field background images). When I change from 3.5" screen to 4", the text fields don't change in the same way that the background resizes. The bg image simply resizes to fill the screen, but the text fields jump out of alignment.
Is there a way to have two sets of constraints, one for each screen size? or is there a way to have views resize proportionally to another view?
EDIT:
Is there a way to have two sets of constraints, one for each screen
size?
Yes, by adding constraints programatically and checking [[UIScreen mainScreen] bounds] to get the screen size.
or is there a way to have views resize proportionally to another view?
Yes, you could set this up in interface builder. But it will be hard to manage, I would manage the constraints manually in code since you are using a custom background image the textviews need to position exactly
You're going to struggle to get these things to line up properly with the text view backgrounds being part of the background image.
You should amend your image assets and use the background property of UITextField to have an actual background image, and remove the boxes from your main background image. The icons could be separate images as well.
Failing that, it would make more sense to have the image stretch underneath the text boxes rather than on top.
Your layout is doing what it should do based on your description, but the image isn't stretching in the right way. In your screenshots, username is always the same distance from the top, and the others are the same distance from the bottom, but that isn't how image stretching works. I don't know how you've set it up but it would make sense to have a single image the size of the 4 inch screen, which has the bottom cut off for 3.5 inch devices, and constrain everything from the top.

Views get corrupt when NSWindow goes to Full-screen (window's min-size violated by animation)

My main NSWindow contains UI restricted to some size range, otherwise it can get corrupt. I restrict the window to a size-range using
[myWindow setContentsMaxSize:maxSize]
[myWindow setContentsMinSize:minSize]
This works fine for user dragging of the edges or size-box.
When the user presses "fullscreen" button, Lion starts an animation that will
Shrink the window below its current size,
in several steps, increase its size until it reaches the full-screen representation size.
If the window started in its minimal size, this animation will shrink it BELOW the defined minimal size, and will corrupt my UI beyond repair (user needs to relaunch the app). My views are receiving setFrameSize: with unsupported size.
My questions
Can this be considered a Cocoa bug?
Am I doing something wrong in my view hierarchy?
Can I somehow prevent the corruption, without replacing the OS standard animation for full-screen?
Why doesn't the standard animation base on a "snapshot" of the window contents, instead of
live-resizing of the whole view-hierarchy throughout the animation? This is surely not efficient.
Is there a simple way to apply another standard transition that will
be non-destructive for me?
Can someone "spare" a few lines of code that will do a simple linear resizing animation that will NOT go below minimum?
Thanks.!
I've also investigated fullscreen animation behaviour and here is how it works:
It is also based on taking snapshots of window's content, but with some improvements. It takes several snapshots on some control points: 512, 1024, 2048 and so on.
In my case to enter full screen 2560x1440, my 400 pixels wide window took 512 pixels snapshot, then 1024 and then 2560 wide snapshot. I don't know whether this behaviour is default for all cases, but this is the result of my own investigation.
On the issue with setting min/max window size:
Minimal window size set up in Interface Builder works for me, but max constraints not. I'm currently working on it and this documented method should work for you:
Place this code into your window delegate.
static const NSSize kWindowMinSize = {100, 100};
static const NSSize kWindowMaxSize = {100500, 100500};
...
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
frameSize.width = MAX(kWindowMinSize.width, MIN(kWindowMaxSize.width, frameSize.width));
frameSize.height = MAX(kWindowMinSize.height, MIN(kWindowMaxSize.height, frameSize.height));
return frameSize;
}
Hope this will help you!

Change bounds origin + cropping an image

I am a newbie to Cocoa, I have a few doubts regarding NSImage.
Question1:
Changing the bounds origin of an image doesn't seem to have any effect. I expected the image to be drawn from the newly set origin but that doesn't seem to the case. Am I missing something ?
code:
NSImage* carImage = [NSImage imageNamed:#"car"];
[self.imageView setImage:carImage];
//Following line has no effect:
self.imageView.bounds = CGRectMake(self.imageView.bounds.origin.x + 100, self.imageView.bounds.origin.y, self.imageView.bounds.size.width,self.imageView.bounds.size.height);
Note: imageView is an IBOutlet
Question2:
I was trying to crop an image, but it doesn't seem to be cropping the image, I can see the complete image. What is that I am missing ?
code:
NSRect sourceRect = CGRectMake(150, 25, 100, 50);
NSRect destRect = CGRectMake(0, 0, 100, 50);
NSImage* carImage = [NSImage imageNamed:#"car"];
[carImage drawInRect:destRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0];
[self.imageView setImage:carImage];
Thanks
Changing the bounds origin of an image doesn't seem to have any effect. …
//Following line has no effect:
self.imageView.bounds = CGRectMake(self.imageView.bounds.origin.x + 100, self.imageView.bounds.origin.y, self.imageView.bounds.size.width,self.imageView.bounds.size.height);
That's an image view, not an image.
The effect of changing the bounds of a view depends on what the view does to draw. Effectively, this means you shouldn't change the bounds of a view that isn't an instance of a view class you created, since you can't predict exactly how an NSImageView will draw its image (presumably, since it's a control, it involves its cell, but more than that, I wouldn't rely on).
More generally, it's pretty rare to change a view's bounds origin. I don't remember having ever done it, and I can't think of a reason off the top of my head to do it. Changing its bounds size will scale, not crop.
I was trying to crop an image, but it doesn't seem to be cropping the image, I can see the complete image. What is that I am missing ?
[carImage drawInRect:destRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0];
[self.imageView setImage:carImage];
Telling an image to draw does not change anything about the image. It will not “crop the image” such that the image will thereafter be smaller or larger. You are telling it to draw, nothing more.
Consequently, the statement after that sets the image view's image to the whole image, exactly as if you hadn't told the image to draw, because telling it to draw made no difference.
What telling an image to draw does is exactly that: It tells the image to draw. There are only two correct places to do that:
In between lockFocus and unlockFocus messages to a view or image (or after setting the current NSGraphicsContext).
Within a view's drawRect: method.
Anywhere else, you should not tell any Cocoa object to draw.
One correct way to crop an image is to create a new image of the desired/adjusted size, lock focus on it, draw the desired portion of the original image into it, and unlock focus on the new image. You will then have both the original and a cropped version.
Another correct way would be to create your own custom image view that has two properties: One owning an image to draw, and the other holding a rectangle. When told to draw, this custom view would tell the image to draw the given rectangle into the view's bounds. You would then always hold the original image and simply draw only the desired section.

Resources