如何使用Swift水平翻转UIImage?

时间:2014-07-25 22:51:45

标签: ios swift uiimage flip

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

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

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

9 个答案:

答案 0 :(得分:27)

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

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

用法如下:

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

答案 1 :(得分:24)

在斯威夫特......(6.3.1)

YourUIImage.transform = CGAffineTransformMakeScale(-1, 1)

这也适用于UIView

答案 2 :(得分:17)

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

Swift 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
}

答案 3 :(得分:10)

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

UIImage

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

答案 4 :(得分:5)

Swift 4

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

答案 5 :(得分:1)

要快速翻转图片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
    }

}


}

答案 6 :(得分:0)

关于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)
    }
}

答案 7 :(得分:0)

Swift 5解决方案

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

extension UIImage {
    func flipHorizontally() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.size, true, 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()

答案 8 :(得分:0)

快捷键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)
    }
  }
}