Skip to content

Instantly share code, notes, and snippets.

@polok polok/file.swift
Created Jan 12, 2017

Embed
What would you like to do?
Small function to rotate the image clockwise or counterclockwise
func rotate(image: UIImage, clockwise: Bool) -> UIImage {
var rotatedImage: UIImage?
let rotateSize = CGSize(width: image.size.height, height: image.size.width)
UIGraphicsBeginImageContextWithOptions(rotateSize, false, 1.0)
guard let context = UIGraphicsGetCurrentContext() else {
return image
}
if clockwise {
CGContextRotateCTM(context, CGFloat(M_PI_2))
CGContextTranslateCTM(context, 0, -image.size.height)
} else {
CGContextRotateCTM(context, CGFloat(-M_PI_2))
CGContextTranslateCTM(context, -image.size.width, 0)
}
image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return rotatedImage ?? image
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.