This is my solution written in **Swift 2.x**, usually I prefeer to use `landscape` mode for my games, so I write this extension:
extension SKTexture {
class func flipImage(name:String,flipHoriz:Bool,flipVert:Bool)->SKTexture {
if !flipHoriz && !flipVert {
return SKTexture.init(imageNamed: name)
}
let image = UIImage(named:name)
UIGraphicsBeginImageContext(image!.size)
let context = UIGraphicsGetCurrentContext()
if !flipHoriz && flipVert {
// Do nothing, X is flipped normally in a Core Graphics Context
// but in landscape is inverted so this is Y
} else
if flipHoriz && !flipVert{
// fix X axis but is inverted so fix Y axis
CGContextTranslateCTM(context, 0, image!.size.height)
CGContextScaleCTM(context, 1.0, -1.0)
// flip Y but is inverted so flip X here
CGContextTranslateCTM(context, image!.size.width, 0)
CGContextScaleCTM(context, -1.0, 1.0)
} else
if flipHoriz && flipVert {
// flip Y but is inverted so flip X here
CGContextTranslateCTM(context, image!.size.width, 0)
CGContextScaleCTM(context, -1.0, 1.0)
}
CGContextDrawImage(context, CGRectMake(0.0, 0.0, image!.size.width, image!.size.height), image!.CGImage)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return SKTexture(image: newImage)
}
}
**Usage:**
let spriteTxt = SKTexture.flipImage(imageName, flipHoriz: true, flipVert: false)
P.S.: If you want the same function but to `portrait` mode please refeer to this [answer][1]
[1]:
[To see links please register here]