ios 如何使用 Swift 水平翻转 UIImage?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24965638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:34:05  来源:igfitidea点击:

How to flip UIImage horizontally with Swift?

iosswiftuiimageflip

提问by Lim Thye Chean

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

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

[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!

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

采纳答案by drewag

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:

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

    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)

回答by duyn9uyen

In Swift.... (6.3.1)

在 Swift 中......(6.3.1)

YourUIImage.transform = CGAffineTransformMakeScale(-1, 1)

This also works with a UIView

这也适用于 UIView

回答by Francesco D.M.

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

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

let flippedImage = straightImage.withHorizontallyFlippedOrientation()

let flippedImage = straightImage.withHorizontallyFlippedOrientation()

Simple one-liners always make me happy :)

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

回答by Alex Shubin

Changing image orientation parameter is not actually flipping the image in all cases. Image must be redrawn somehow... For example like this:

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

Swift 3

斯威夫特 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
}

回答by Alfi

Swift 4

斯威夫特 4

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

回答by Martin Pilch

There is couple of similar questions on SO. And I believe it's worth to post solution using CoreImagehere too.

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

Please note: when getting final UIImage, it's necessary to convert to CGImagefirst to respect extent of CIImage

请注意:获得 final 时UIImage,有必要转换为CGImagefirst 以尊重范围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)
    }
}

回答by Josh Bernfeld

Swift 5 Solution

Swift 5 解决方案

Here is the simplest solution for actually flipping the image

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

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

and to use this solution you can do the following

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

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

回答by Faizal Nowshad KN

To flip image in swift 4

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

}


}