Re: Resizing NSimage
Re: Resizing NSimage
- Subject: Re: Resizing NSimage
- From: Yann Bizeul <email@hidden>
- Date: Thu, 25 Nov 2004 15:32:50 +0100
So to improve it, I'm looking at Cocoa functions and it seems possible
to draw the image at different sizes. I guess I could use Quartz to
draw to an offscreen image?? Then access that image.
Before going down that path, any simpler / better
suggestions...something like producing a resized bitmap directly from
the original one...
Here is a code I use to resample an image, it produces good quality
thumbnails.
This is adapted from a sample found somewhere in the archives.
=== CUT HERE ===
NSSize thumbSize = NSMakeSize(200,200);
NSImage *tiff = [[NSImage alloc]initWithContentsOfFile:[self
valueForKey:@"file"]];
float bigRatio = [tiff size].width / [tiff size].height;
float thumbRatio = thumbSize.width / thumbSize.height;
NSSize smallSize;
if (bigRatio > thumbRatio)
smallSize = NSMakeSize(thumbSize.width,thumbSize.width/bigRatio);
else
smallSize = NSMakeSize(thumbSize.height*bigRatio,thumbSize.height);
NSBitmapImageRep *imageRepA = [[tiff
representations]objectAtIndex:0];
NSBitmapImageRep *imageRep = [imageRepA representationWithSize:
smallSize];
NSDictionary *properties = [NSDictionary dictionaryWithObject:
[NSNumber numberWithFloat: 1.0] forKey: NSImageCompressionFactor];
[[imageRep representationUsingType:NSJPEGFileType
properties:properties]writeToFile:outJPEGFile atomically:YES];
=== CUT HERE ===
This rely on a NSBitmapImageRep category :
------------------------------------------
=== CUT HERE ===
@implementation NSBitmapImageRep (Sizing)
- (NSBitmapImageRep *)representationWithSize:(NSSize)size
{
NSRect rect = NSMakeRect(0, 0, size.width, size.height);
NSImage *image = [[[NSImage alloc] initWithSize:size] autorelease];
NSBitmapImageRep *outRep;
[image lockFocus];
[[NSGraphicsContext
currentContext]setImageInterpolation:NSImageInterpolationHigh];
[self drawInRect:rect];
outRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:rect]
autorelease];
[image unlockFocus];
return outRep;
}
@end
=== CUT HERE ===
--
Yann Bizeul - yann at tynsoe.org
Please use this e-mail when writing to me.
You can visit my projects at this address :
http://projects.tynsoe.org/
(BuddyPop - GeekTool - SSH Tunnel Manager...)
_______________________________________________
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