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