LoginSignup
8
6

More than 5 years have passed since last update.

【Swift】UIImageを左右反転させる

Posted at

こんな感じで実現できました。

UIImageExtension.swift
extension UIImage {
    func flipHorizontal() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        let imageRef = self.CGImage
        let context = UIGraphicsGetCurrentContext()
        CGContextTranslateCTM(context, size.width, size.height)
        CGContextScaleCTM(context, -1.0, -1.0)
        CGContextDrawImage(context, CGRect(x: 0, y: 0, width: size.width, height: size.height), imageRef)
        let flipHorizontalImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return flipHorizontalImage
    }
}

これも同様に結構使えました。

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6