【问题标题】:How to flip UIImage horizontally with Swift?如何使用 Swift 水平翻转 UIImage?
【发布时间】:2014-09-17 21:26:02
【问题描述】:

UIImage翻转的解决方案是使用Objective-C代码:

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

然而,imageWithCGImage 在 Swift 中不可用!是否有使用 Swift 水平翻转图像的解决方案?谢谢!

【问题讨论】:

    标签: ios swift uiimage flip


    【解决方案1】:

    对我来说,最简单的方法是使用UIImage.withHorizontallyFlippedOrientation() 实例方法,如下所示:

    let flippedImage = straightImage.withHorizontallyFlippedOrientation()

    简单的单行字总是让我开心:)

    【讨论】:

    • 很棒,但不尊重从 AVFoundation 捕获的图像的垂直方向 - 将图像上下翻转
    • 也许有办法在withHorizontallyFlippedOrientation()之后调整这个?
    • #available(iOS 10.0, *)
    【解决方案2】:

    大多数工厂方法都在 swift 中转换为初始化程序。只要可用,即使类方法仍然可用,它们也是首选。您可以使用:

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

    用法如下所示:

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

    斯威夫特 5

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

    【讨论】:

    • 试过这个,它似乎没有翻转图像。渲染时有没有办法尊重方向? public extension UIImage { var flippedHorizontally: UIImage { return UIImage(CGImage: self.CGImage!, scale: self.scale, orientation: .UpMirrored) } }
    • 哦...我使用 .LeftMirrored 选项拍摄自拍照片。翻转后显示照片效果很好。但是,保存到相册后,照片是颠倒的。有什么想法吗?
    • @Gujamin,该扩展程序运行良好。我将它转换为 Swift 3 语法,并且能够毫无问题地交换 UIButton 的图像。谢谢。
    【解决方案3】:

    在 Swift 中.... (6.3.1)

    YourUIImage.transform = CGAffineTransformMakeScale(-1, 1)
    

    这也适用于 UIView

    【讨论】:

    • 这不是用来翻转UIImage,而是用来翻转UIImageView
    • 它没有翻转 UIImage。但它解决了我的 UI 问题。
    【解决方案4】:

    在所有情况下,更改图像方向参数实际上并不是翻转图像。图像必须以某种方式重绘......例如这样:

    斯威夫特 3

    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
    }
    

    【讨论】:

      【解决方案5】:

      斯威夫特 4

      YOURIMAGEVIEW.transform = CGAffineTransform(scaleX: -1, y: 1)
      

      【讨论】:

      • 问题是关于 UIImage,而不是 UIImageView
      • 这不是正确的答案,但在其他情况下它对我有用,谢谢
      【解决方案6】:

      Swift 5 解决方案

      这是实际翻转图像的最简单解决方案

      extension UIImage {
          func flipHorizontally() -> UIImage? {
              UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
              let context = UIGraphicsGetCurrentContext()!
              
              context.translateBy(x: self.size.width/2, y: self.size.height/2)
              context.scaleBy(x: -1.0, y: 1.0)
              context.translateBy(x: -self.size.width/2, y: -self.size.height/2)
              
              self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
              
              let newImage = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
              
              return newImage
          }
      }
      

      要使用此解决方案,您可以执行以下操作

      let image = UIImage(named: "image.png")!
      let newImage = image.flipHorizontally()
      

      【讨论】:

      • 这会创建黑色背景图片而不是透明图片。请注意
      • @RajuyourPepe 我已经更新了答案以提高透明度。谢谢!
      • 很好的答案!对于任何想知道的人,如果您想垂直翻转图像,只需将 context.scaleBy(x: -1.0, y: 1.0) 更改为 context.scaleBy(x: 1.0, y: -1.0)
      【解决方案7】:

      在swift 4中翻转图像

      class ViewController: UIViewController {
      var first = 1
      var second = 1
      
      @IBOutlet weak var img: UIImageView!
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
      }
      
      @IBAction func flip1(_ sender: Any) {
      
          if first == 1 {
              img.transform = CGAffineTransform(scaleX: -1, y: 1)
              first = 2
          }
          else if first == 2
          {
              img.transform = CGAffineTransform(scaleX: +1, y: 1)
              first = 1
          }
      
      }
      
      
      }
      

      【讨论】:

        【解决方案8】:

        关于 SO 有几个类似的问题。我相信也值得在这里使用CoreImage 发布解决方案。

        请注意:当得到最终的UIImage时,必须先转换为CGImage以尊重CIImage的范围

        extension UIImage {
            func imageRotated(by degrees: CGFloat) -> UIImage {
        
                let orientation = CGImagePropertyOrientation(imageOrientation)
                // Create CIImage respecting image's orientation 
                guard let inputImage = CIImage(image: self)?.oriented(orientation) 
                    else { return self }
        
                // Flip the image itself
                let flip = CGAffineTransform(scaleX: 1, y: -1)
                let outputImage = inputImage.transformed(by: flip)
        
                // Create CGImage first
                guard let cgImage = CIContext().createCGImage(outputImage, from: outputImage.extent) 
                    else { return self }
        
                // Create output UIImage from CGImage
                return UIImage(cgImage: cgImage)
            }
        }
        

        【讨论】:

          【解决方案9】:

          斯威夫特 5

          如果您的用例需要在转换后保存UIImage,则需要将其绘制成新的CGContext

          extension UIImage {
            
            enum Axis {
              case horizontal, vertical
            }
            
            func flipped(_ axis: Axis) -> UIImage {
              let renderer = UIGraphicsImageRenderer(size: size)
              
              return renderer.image {
                let context = $0.cgContext
                context.translateBy(x: size.width / 2, y: size.height / 2)
                
                switch axis {
                case .horizontal:
                  context.scaleBy(x: -1, y: 1)
                case .vertical:
                  context.scaleBy(x: 1, y: -1)
                }
                
                context.translateBy(x: -size.width / 2, y: -size.height / 2)
                
                draw(at: .zero)
              }
            }
          }
          

          【讨论】:

          • 使用它:imgClone.image =pickImage.flipped(.vertical)
          猜你喜欢
          • 1970-01-01
          • 2022-08-20
          • 1970-01-01
          • 2019-07-01
          • 2012-02-17
          • 1970-01-01
          • 2018-01-05
          • 1970-01-01
          相关资源
          最近更新 更多