Flip UIImage Along Either Axis

后端 未结 5 1511
余生分开走
余生分开走 2021-01-13 17:05

I\'m trying to create a method which flips a UIImage along the X axis, Y axis, or both. I keep getting close but my transform knowledge isn\'t good enough to get all the way

5条回答
  •  离开以前
    2021-01-13 17:47

    This is merely a slightly fixed and 'updated to Swift' version of the old answer by @ultramiraculous. Just in case it helps someone.

    Vertical Flip (reflection over x-axis):

    func flipV(im:UIImage)->UIImage {
        var newOrient:UIImageOrientation
        switch im.imageOrientation {
        case .Up:
            newOrient = .DownMirrored
        case .UpMirrored:
            newOrient = .Down
        case .Down:
            newOrient = .UpMirrored
        case .DownMirrored:
            newOrient = .Up
        case .Left:
            newOrient = .LeftMirrored
        case .LeftMirrored:
            newOrient = .Left
        case .Right:
            newOrient = .RightMirrored
        case .RightMirrored:
            newOrient = .Right
        }
        return UIImage(CGImage: im.CGImage, scale: im.scale, orientation: newOrient)
    }
    

    Horizontal Flip (reflection over y-axis)

    func flipH(im:UIImage)->UIImage {
        var newOrient:UIImageOrientation
        switch im.imageOrientation {
        case .Up:
            newOrient = .UpMirrored
        case .UpMirrored:
            newOrient = .Up
        case .Down:
            newOrient = .DownMirrored
        case .DownMirrored:
            newOrient = .Down
        case .Left:
            newOrient = .RightMirrored
        case .LeftMirrored:
            newOrient = .Right
        case .Right:
            newOrient = .LeftMirrored
        case .RightMirrored:
            newOrient = .Left
        }
        return UIImage(CGImage: im.CGImage, scale: im.scale, orientation: newOrient)
    }
    

提交回复
热议问题