Resizing an NSImage (and getting good-looking results!)
Resizing an NSImage (and getting good-looking results!)
- Subject: Resizing an NSImage (and getting good-looking results!)
- From: Toby Atkin-Wright <email@hidden>
- Date: Tue, 25 Jan 2005 15:31:49 +0000
I want to load a JPEG, reduce it in size, and convert the new image to
a JPEG ready for upload to a web site. The following code works, but
the results look unpleasantly rough:
// load and prepare image
NSImage* theImage = [[NSImage alloc]
initWithContentsOfFile:theFilename];
[theImage setScalesWhenResized:YES];
// resize the image
[theImage lockFocus];
[[NSGraphicsContext currentContext]
setImageInterpolation:NSImageInterpolationHigh]; // does this do
anything?
[theImage setSize:theNewSize]; // theNewSize is an NSSize struct
[theImage unlockFocus];
// convert it back to a JPEG
NSData* theImageDataTemp = [theImage TIFFRepresentation];
NSBitmapImageRep* theImageRepresentation = [NSBitmapImageRep
imageRepWithData:theImageDataTemp];
NSDictionary* theProperties = [NSDictionary
dictionaryWithObject:[NSNumber numberWithFloat:0.9]
forKey:NSImageCompressionFactor];
NSData* theImageData = [theImageRepresentation
representationUsingType:NSJPEGFileType properties:theProperties];
I guess its not doing any anti-aliasing when resizing the image. I was
hoping that calling setImageInterpolation:NSImageInterpolationHigh on
the graphics context would help, but it doesn't seem to make any
difference.
What should I be doing?
Toby
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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