tags:

views:

21

answers:

1

Hi,

In Mac OSX, I have an image with black pixel in all 4 directions.

I want to programmatically crop the image to the maximum image rect.

Should i check for the black pixel and then create the crop rect or is there any supported API is there?

+1  A: 

Create an NSImage of the desired size, lock focus on it, draw the desired crop rectangle of the source image into the whole bounds of the destination image, and unlock focus. The image you created now contains the crop from the source image.

Note that this will lose information like resolution (DPI), color profile, and EXIF tags. If you want to preserve those things (probably a good idea), use CGImage:

  1. Use CGImageSource to load the image. Be sure to recover the properties of each image from the file, as well as the images themselves. And note that I used the plural: TIFF files can contain multiple images.
  2. Use the CGImageCreateWithImageInRect function to crop out the desired section of each image. Don't forget to release each original image as appropriate.
  3. If you want to write the cropped-out images to a file, do so using CGImageDestination. Pass both the images and the attributes dictionaries you obtained in step 1.
Peter Hosey
Thanks Peter,But how to get the crop rect?The actual image 300X200, which may not be in a rectangle shape in the full image of 1200X600. I want to get the crop rect which covers this entire (300X200) image part.I have done this by going through each pixel of all rows and columns of the image. Is there any other approach than this?
Dhanaraj
........................
Dhanaraj
I already told you how to extract the desired section of the image. The rectangle is easy to figure out, since you know the width and height and that it's at the center. There is no need to examine the pixels yourself.
Peter Hosey