Re: NSImage rescaling
Re: NSImage rescaling
- Subject: Re: NSImage rescaling
- From: John Pannell <email@hidden>
- Date: Mon, 7 Feb 2005 21:28:25 -0700
Hi David-
I think you may be bumping up against a limitation of NSImageView in regard to NSScaleProportionally. In my experience, it will shrink an image proportionally, but not grow one. Once you reach the original size of the image, the image just sits centered, actual size, in the growing view.
Not knowing the mechanics of your app, I don't know if the following snippet will work for you or not. I have an NSImageView subclass that sits in an NSScrollView, an have provided the user with a pop-up control on the scroll view to select scaling for the image (much like Freehand, Photoshop, etc.). In this case, the NSImageView subclass uses NSScaleToFit, looks at its superview to determine the target size, and then ensures that it resizes itself to the same aspect ratio as the image. This prevents you from having to monkey with another copy of the image, etc. In the code below, -1 is sent in as the "proportionally scale to fit", otherwise scale is a float representing the percent scale. The code grows the NSImageView (subclass) - it never does anything with the NSImage.
You can see it in action: http://www.positivespinmedia.com/shareware/NetScrape/
It should also be noted that enlarging an image is by nature going to result in a degraded image, since you now have less image data than you have image. Hope this helps!
@implementation PSMFlippedImageView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setImageScaling:NSScaleToFit];
}
return self;
}
- (BOOL)isFlipped
{
return YES;
}
- (float)scale
{
NSSize imageSize = [[self image] size];
float heightFactor, widthFactor;
NSAffineTransform *at = [NSAffineTransform transform];
if(scale == -1.0){
// size to fit
heightFactor = [[self superview] frame].size.height/imageSize.height;
widthFactor = [[self superview] frame].size.width/imageSize.width;
if(heightFactor > widthFactor){
scale = widthFactor;
} else {
scale = heightFactor;
}
}
[at scaleBy:scale];
[self setFrameSize:[at transformSize:imageSize]];
[self setNeedsDisplay:YES];
}
- (void)resizeWithOldSuperviewSize:(NSSize)oldFrameSize
{
// override to prevent the stretching of the image. No implementation needed.
}
@end
John
On Feb 7, 2005, at 3:57 PM, David Piasecki wrote:
<x-tad-smaller>I've been reading around online to try and get to the bottom of what seems to be a common problem. I'm scaling an NSImage object using the following code:
</x-tad-smaller> NSImage *scaledImage = [image copy];
[scaledImage setScalesWhenResized:YES];
NSSize imageSize = [scaledImage size];
[scaledImage setSize:NSMakeSize(imageSize.width*zoom, imageSize.height*zoom)];
[imageView setImage:scaledImage];
[scaledImage release];
This works well; however, it is incredibly slow when the image gets very large. So rather than copy the image every time I want to enlarge or shrink the image in my NSImageView, I want the image to automatically rescale itself. So I decided to manually change the size of my NSImageView object and set the scale property to NSScaleProportionally. This doesn't appear to have any effect. Although the NSImageView size changes, the NSImage displayed remains the same size unless I copy the image and call [imageView setImage:scaledImage] again as in the code above. Another behavior I've observed in different attempts to solve this problem is to have the NSImage be displayed at a very poor resolution after zooming out and then trying to zoom back in again since NSImage's setSize method seems to cause the loss of information.
I'm sure this is really simple and that I'm going about it the wrong way, so I'd really appreciate it if someone could help by explaining how to have one image and simply scale it up or down based on the size of the NSImageView object which contains it.
Thanks,
David _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden