Flip CIImage Horizontal
Asked Answered
A

5

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

Aragonite answered 1/5, 2016 at 12:50 Comment(1)
why don't you try this UIImage* flippedImage = [UIImage imageWithCGImage:imgV.image.CGImage scale:imgV.image.scale orientation:UIImageOrientationUpMirrored];Reinold
A
10

Try this way:

image = [image imageByApplyingTransform:CGAffineTransformMakeScale(-1, 1)];
image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation(0, sourceExtent.size.height)];
Ascent answered 1/5, 2016 at 13:12 Comment(1)
You meant to the second line to be: image = [image imageByApplyingTransform:CGAffineTransformMakeTranslation(sourceExtent.size.width,0)];Dobsonfly
H
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)
Haines answered 2/1, 2018 at 22:5 Comment(2)
I get the error message "'oriented' is only available on iOS 11.0 or newer"Drat
image.oriented(.upMirrored) only works for iOS 11+, how can I implement it under iOS 11?Meditation
K
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)];
Kythera answered 13/10, 2016 at 16:25 Comment(0)
R
1

This what worked for me:

UIImage(ciImage: ciImage.oriented(.upMirrored).oriented(.left))
Ranite answered 8/5, 2019 at 6:3 Comment(0)
D
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];
Deneb answered 4/10, 2022 at 0:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.