Re: Image Scaling Code
Re: Image Scaling Code
- Subject: Re: Image Scaling Code
- From: Tom Waters <email@hidden>
- Date: Fri, 13 Jul 2001 22:25:45 -0700
On Friday, July 13, 2001, at 09:52 PM, Michael Horn wrote:
>
>
Anyone know of an example of quality image scaling code? Perhaps using
>
Quicktime?
>
>
What I have is a fairly large JPG (1600 x 1200) and I would like to
>
scale it down and create a thumbnail for it.
You need to use drawInRect... here's some code (written in email and not
tested) that might work:
const int thumbMax = 128;
NSImage *image = [NSImage imageNamed: @"myBigImage.jpg"];
NSSize size = [image size];
NSSize thumbSize = size.width > size.height ?
NSMakeSize(thumbMax, thumbMax * size.height / size.width) :
NSMakeSize(thumbMax * size.width / size.height, thumbMax);
NSImage *thumbnail = [[NSImage alloc] initWithSize: thumbSize];
[thumbnail lockFocus];
[image drawInRect: NSMakeRect(0, 0, thumbSize.width, thumbSize.height)
fromRect: NSMakeRect(0, 0, [image size].width, [image size].height)
operation: NSCompositeCopy fraction: 1.0];
[thumbnail unlockFocus];
Now you should have the thumbnail with the right pixels in it, i'll
leave it as an exercise to display it and/or save it to disk. (there
might be some issue with the image being upside-down or drawn in the
wrong place... depending on if your view and/or the images are
"flipped", see isFlipped)