Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flip UIImage horizontally with Swift?

The solution to do UIImage flipping is with the Objective-C code:

[UIImage imageWithCGImage:img.CGImage scale:1.0 orientation: UIImageOrientationDownMirrored] 

However, imageWithCGImage is not available in Swift! Is there a solution for flipping image horizontally with Swift? Thanks!

like image 377
Lim Thye Chean Avatar asked Jul 25 '14 22:07

Lim Thye Chean


People also ask

How do you flip an image in Swift?

Swift 5 Solution For anyone wondering, if you want to flip the image vertically just change context. scaleBy(x: -1.0, y: 1.0) to context. scaleBy(x: 1.0, y: -1.0) .

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


1 Answers

For me the simplest way was to use the .withHorizontallyFlippedOrientation() instance method of UIImage as follows:

let flippedImage = straightImage.withHorizontallyFlippedOrientation()

Simple one-liners always make me happy :)

like image 181
Francesco D.M. Avatar answered Oct 05 '22 20:10

Francesco D.M.