6

I am trying to flip my CIImage Horizontal with :

image = [image imageByApplyingTransform:CGAffineTransformMakeScale(1, -1)];
image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation(0, sourceExtent.size.height)];

But i always get the image flip vertical instead

rmaddy
  • 314,917
  • 42
  • 532
  • 579
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • why don't you try this UIImage* flippedImage = [UIImage imageWithCGImage:imgV.image.CGImage scale:imgV.image.scale orientation:UIImageOrientationUpMirrored]; – Teja Nandamuri May 01 '16 at 15:20

5 Answers5

10

Try this way:

image = [image imageByApplyingTransform:CGAffineTransformMakeScale(-1, 1)];
image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation(0, sourceExtent.size.height)];
Alex Kosyakov
  • 2,095
  • 1
  • 17
  • 25
  • You meant to the second line to be: image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation(sourceExtent.size.width,0)]; – Ken Aspeslagh May 09 '22 at 20:03
7

This will properly flip the image horizontally and maintain proper coordinates.

In Obj-C:

image = [image imageByApplyingCGOrientation: kCGImagePropertyOrientationUpMirrored];

In Swift:

image = image.oriented(.upMirrored)

https://developer.apple.com/documentation/coreimage/ciimage/2919727-oriented

If what you want is to orient it in such a way that you can use it in an UIImage, then you want to flip it in both axes.

image = image.oriented(.downMirrored)
Luis
  • 171
  • 1
  • 8
2

Alex almost had it, but you need to adjust the translation as well:

image = [image imageByApplyingTransform:CGAffineTransformMakeScale(-1, 1)];
image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation(sourceExtent.size.width, 0)];
Kai Schaller
  • 443
  • 3
  • 9
1

This what worked for me:

UIImage(ciImage: ciImage.oriented(.upMirrored).oriented(.left))
Rotem
  • 2,306
  • 3
  • 26
  • 44
0

This will allow you to flip a UIImage (or CIImage) in either horizontal or vertical directions:

UIImage *image;
BOOL mirrorX, mirrorY;


CIImage *ciimage = [CIImage imageWithCGImage:image.CGImage];
ciimage = [ciimage imageByApplyingTransform:CGAffineTransformMakeScale(mirrorX?-1:1, mirrorY?-1:1)];
ciimage = [ciimage imageByApplyingTransform:CGAffineTransformMakeTranslation(mirrorX?image.size.width:0, mirrorY?image.size.height:0)];
image = [UIImage imageWithCIImage:ciimage];
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195