How to Flip Uiimage Horizontally

How to flip UIImage horizontally?

Objective-C

UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];

UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUpMirrored];

Swift

let flippedImage = myImage.withHorizontallyFlippedOrientation()

How to flip UIImage horizontally with Swift?

Most factory methods are converted to initializers in swift. Whenever available, even if the class method is still available, they are preferred. You can use:

    init(CGImage cgImage: CGImage!, scale: CGFloat, orientation: UIImageOrientation)

The usage would look like this:

var image = UIImage(CGImage: img.CGImage, scale: 1.0, orientation: .DownMirrored)

Swift 5

var image = UIImage(cgImage: img.cgImage!, scale: 1.0, orientation: .downMirrored)

Flipping a UIImage horizontally in UIImageView

Problem solved. Rather than flipping the UIImage, I flipped the UIImageView as follows:

self.myImageView.transform = CGAffineTransformMakeScale(-1, 1);

How to flip image horizontally to save flipped in photo album?

From Apple's docs :

Image orientation affects the way the image data is displayed when drawn.

So that doesn't actually affect the image data.

If you just want to "flip the orientation" in the image's metadata, you can use .withHorizontallyFlippedOrientation() and then just save the image (instead of translating it to png data and back).

If you really want the image data (the pixels) flipped, you need to draw it flipped and then save that image. One way to do it:

func flipImageLeftRight(_ image: UIImage) -> UIImage? {

UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: image.size.width, y: image.size.height)
context.scaleBy(x: -image.scale, y: -image.scale)

context.draw(image.cgImage!, in: CGRect(origin:CGPoint.zero, size: image.size))

let newImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return newImage
}

Flip UIImage Along Either Axis

I finally was able to figure this out. Here is the code that works for anyone else who might need it.

- (UIImage *)flippedImageByAxis:(MVImageFlip)axis{
UIGraphicsBeginImageContext(self.size);
CGContextRef context = UIGraphicsGetCurrentContext();

if(axis == MVImageFlipXAxis){
// Do nothing, X is flipped normally in a Core Graphics Context
} else if(axis == MVImageFlipYAxis){
// fix X axis
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);

// then flip Y axis
CGContextTranslateCTM(context, self.size.width, 0);
CGContextScaleCTM(context, -1.0f, 1.0f);
} else if(axis == MVImageFlipXAxisAndYAxis){
// just flip Y
CGContextTranslateCTM(context, self.size.width, 0);
CGContextScaleCTM(context, -1.0f, 1.0f);
}

CGContextDrawImage(context, CGRectMake(0.0, 0.0, self.size.width, self.size.height), [self CGImage]);

UIImage *flipedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return flipedImage;
}

Flip CIImage Horizontal

Try this way:

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


Related Topics



Leave a reply



Submit