Resize and Save NSImage?

Josh Kahane picture Josh Kahane · Mar 10, 2011 · Viewed 10k times · Source

I have an NSImageView which I get an image for from an NSOpenPanel. That works great.

Now, how can I take that NSImage, half its size and save it as the same format in the same directory as the original as well?

If you can help at all with anything I'd appreciate it, thanks.

Answer

Anne picture Anne · Mar 10, 2011

Check the ImageCrop sample project from Matt Gemmell:
http://mattgemmell.com/source/

Nice example how to resize / crop images.
Finally you can use something like this to save the result (dirty sample):

// Write to TIF
[[resultImg TIFFRepresentation] writeToFile:@"/Users/Anne/Desktop/Result.tif" atomically:YES];

// Write to JPG
NSData *imageData = [resultImg  TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:@"/Users/Anne/Desktop/Result.jpg" atomically:NO];