Resizing images
Resizing images
- Subject: Resizing images
- From: Luke Burton <email@hidden>
- Date: Thu, 22 Dec 2005 15:27:31 +1100
Dear cocoa-dev people,
I am new to Cocoa and Objective-C, but am a competent C++ programmer.
I have kicked off an open source project called SimpleViewer iPhoto Export.
It exports photos from iPhoto into a directory, and prepares them into a
nice flash gallery format. You can see some examples (and download source
code and binaries) from my homepage:
http://www.hagus.net/taxonomy/term/14
Now. The reason I'm writing is that my resizing code appears to create
pretty jagged images, which is less than desireable! I'm quite lost as to
how I can improve my resize "algorithm", as I really pieced together this
code using trial and error, and various usenet postings. If someone could
suggest some improvements, I would be most grateful.
// Given source file "source", resize the longest/shortest edge to be "size"
long, and write to "destination".
+(NSString*)resizeFile:(NSString*)source destination:(NSString*)destination
size:(int)size scaleLongest:(BOOL)scaleLongest
{
NSFileManager * fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:source])
return [NSString stringWithFormat:@"Unable to find source file
\"%s\"", [source UTF8String]];
NSImage * myImage = [[NSImage alloc] initWithContentsOfFile:source];
NSRect srcRect = {NSZeroPoint, [myImage size]};
float targetWidth = 0, targetHeight = 0;
// SNIP - chopped out the code that actually calculates these values.
printf("Translating %f/%f to %f/%f\n", srcRect.size.width,
srcRect.size.height, targetWidth, targetHeight);
[myImage lockFocus];
[myImage setScalesWhenResized:TRUE];
[myImage setSize:NSMakeSize(targetWidth, targetHeight)];
[myImage drawAtPoint:NSZeroPoint fromRect:srcRect
operation:NSCompositeCopy fraction:1.0];
NSBitmapImageRep * imageRep = [[NSBitmapImageRep alloc]
initWith
Data:[myImage TIFFRepresentation]];
NSData * data = [imageRep representationUsingType:NSJPEGFileType
properties:nil];
[data writeToFile:destination atomically:NO];
[myImage unlockFocus];
[imageRep release];
[myImage release];
return nil;
}
--
Luke Burton.
Yes, questions. Morphology, longevity, incept dates.
_______________________________________________
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