开发者

How to Crop image to the maximum image rect?

开发者 https://www.devze.com 2023-01-06 23:18 出处:网络
In Mac OSX, I have an image with black pi开发者_JS百科xel in all 4 directions. I want to programmatically crop the image to the maximum image rect.

In Mac OSX, I have an image with black pi开发者_JS百科xel 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?


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消