views:

60

answers:

2

I am a bit confused about what the best approach is to resize a JPEG file on disk and save the resized JPEG as a new file to disk (on Mac OS X with Cocoa). There are a number of threads about resizing, but I am wondering what approach to use. Do I need to use Core Graphics for this or is this framework "too much" for a simple operation as a resize? Any pointers are welcome as I am a bit lost.

A: 

Install and use ImageMagick.

Yehonatan
Although the question is tagged as a Cocoa-related question, I was probably not clear that I am looking for a Cocoa-based solution.
bare_nature
And whats stopping you from using ImageMagick?
Yehonatan
I'm looking for an approach provided by standard Cocoa frameworks. If possible, I choose not to choose third party solutions. Basically, my question is how to deal with image resizing in Cocoa.
bare_nature
I found this post `Magick++ in Cocoa: http://studio.imagemagick.org/pipermail/magick-users/2004-June/012928.html`
jcubic
As I said, I am looking for a solution based on the standard Cocoa frameworks. Resizing images seems such a trivial operation that I find it hard to imagine that it is a complex operation. I am looking for someone who can point me in the right direction.
bare_nature
Yehonatan, jcubic: Cocoa and Core Graphics can do resizing just fine. Why should the questioner require or bundle ImageMagick?
Peter Hosey
@bare_nature: Scaling an image is not such a trivial operation as you might think. Internally Cocoa creates an off-screen window and draws the image in there at the requested size and caches that then if it needs to scale an image. But there is no public API for this.
Sven
+1  A: 

Core Graphics isn't “too much”; it's the right way to do it.

There is a Cocoa solution:

  1. Create an image of the desired size (the destination image).
  2. Lock focus on it.
  3. Draw the source image into it.
  4. Unlock focus on it.
  5. Export it to desired file format.
  6. Write that data somewhere.

But that destroys metadata.

The Core Graphics solution is not a whole lot different:

  1. Use an image source to load the image and its metadata.
  2. Create a bitmap context of the desired size with the source image's color space. (The hard part here is making sure that the destination context matches the source image as closely as possible while still being in one of the supported pixel formats.)
  3. Draw the source image into it.
  4. Capture the contents of the context.
  5. Use an image destination to write the image and metadata to a file.

And the Core Graphics solution ensures that as little information as possible is lost along the way. (You may want to adjust the DPI metadata, if present.)

Peter Hosey
It took me a while as I'm not familiar with CG, but I got it figured out thanks to your instructions. Thanks very much, Peter!
bare_nature