How to flip UIImage horizontally with Swift?

后端 未结 9 1876
余生分开走
余生分开走 2020-12-13 00:16

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

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


        
相关标签:
9条回答
  • 2020-12-13 00:43

    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:

        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)
    
    0 讨论(0)
  • 2020-12-13 00:46

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

    Please note: when getting final UIImage, it's necessary to convert to CGImage first to respect extent of 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)
        }
    }
    
    0 讨论(0)
  • 2020-12-13 00:47

    Swift 5 Solution

    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()
    
    0 讨论(0)
  • 2020-12-13 00:54

    In Swift.... (6.3.1)

    YourUIImage.transform = CGAffineTransformMakeScale(-1, 1)
    

    This also works with a UIView

    0 讨论(0)
  • 2020-12-13 00:55

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

    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
    }
    
    0 讨论(0)
  • 2020-12-13 00:56

    To flip image in 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
        }
    
    }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题