Flip UIImage Along Either Axis

后端 未结 5 1506
余生分开走
余生分开走 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:53

    Swift 3 version:

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

提交回复
热议问题