• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Resizing images
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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]
initWithData:[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

  • Follow-Ups:
    • Re: Resizing images
      • From: "John C. Randolph" <email@hidden>
    • RE: Resizing images
      • From: "Bas Scheffers" <email@hidden>
    • Re: Resizing images
      • From: Ryan Britton <email@hidden>
  • Prev by Date: Re: How to remove accents from characters (i.e. ñ --> n)
  • Next by Date: Re: Resizing images
  • Previous by thread: CoreData - crashes on 10.4.3, okay on 10.4.2
  • Next by thread: Re: Resizing images
  • Index(es):
    • Date
    • Thread