Cocoa in the Shell

[iOS] Introducing NYXImagesUtilities

I began writing a set of categories for iOS’s UIImage class, because I often need to manipulate images and iOS is missing Core Image so much. So I created a project regrouping all these handy categories, and today I’ll share them with you.

So here it is, NYXImagesUtilities, it’s currently composed of 4 categories, most of the code is using Core Graphics, so you need to link with QuartzCore.framework if you want to use it.

Here is the original image to illustrate my post

1. Filtering

This category allows you to apply filters on a UIImage object, currently there are 3 filters : Sepia, Grayscale and changing opacity.

Using these filters is very easy :

UIImage* sepia = [myImage sepia];
UIImage* gray = [myImage grayscale];
UIImage* transparent = [myImage opacity:0.5f];

2. Masking

This category is composed of a single method which allows to mask an image, you just have to create the mask you desire.

UIImage* masked = [myImage maskWithImage:[UIImage imageNamed:@"mask.png"]];

3. Resizing

This category can be used to crop or to scale images.

Cropping

You can crop your image by 9 different ways :

  1. Top left
  2. Top center
  3. Top right
  4. Bottom left
  5. Bottom center
  6. Bottom right
  7. Left center
  8. Right center
  9. Center

To crop an image just call :

UIImage* cropped = [myImage cropToSize:(CGSize){width, height} usingMode:NYXCropModeCenter];

NYXCropMode is an enum type which can be found in the header file, it is used to represent the 9 modes above.

There is also a convenience method which crop from the top left corner by default : -(UIImage*)cropToSize:(CGSize)newSize;

Scaling

You have the choice between two methods to scale images, the two methods will keep the aspect ratio of the original image.

UIImage* scaled1 = [myImage scaleByFactor:0.5f];
UIImage* scaled2 = [myImage scaleToFitSize:(CGSize){width, height}];

4. Rotating

With this category you can rotate or flip an UIImage object, flipping is useful if you want to create a reflect effect.

UIImage* rotated1 = [myImage rotateInDegrees:217.0f];
UIImage* rotated2 = [myImage rotateInRadians:M_PI_2];
UIImage* flipped1 = [myImage verticalFlip];
UIImage* flipped2 = [myImage forizontalFlip];

The project is hosted on github, and is licensed under the Simplified BSD license.

I hope you will find it useful, and I’m open to any suggestions / ameliorations.

Tags: , ,