How do I mirror a UIImage picture from UIImagePickerController

Terry picture Terry · Jun 28, 2010 · Viewed 24.2k times · Source

I'm trying to figure out if there is any way to mirror an image. For example, take a picture of someone's face and then cut it in half and show what their face looks like with each side mirrored. There doesn't seem to be any tricks like this in CGAffineTransform functions. Graphics experts please help!!!

Answer

warrenm picture warrenm · Jun 28, 2010

The basic "trick" here is to use a scaling transform about the X or Y axis with a factor of -1. For example, you could use this to create a "flip about the horizontal axis" transform:

CGAffineTransform transform = CGAffineTransformScale(transform, -1, 1);

Then you can set the transform property on a UIImageView to flip the assigned image, or concatenate it with another transform to do more sophisticated effects. To get the exact effect you described, you may need to write some custom drawing code to draw your original image into a context, then overlay the flipped half on top of it. This is relatively straightforward in Core Graphics.