Skip to content

Instantly share code, notes, and snippets.

@randhirraj3130
Last active February 4, 2017 07:36
Show Gist options
  • Save randhirraj3130/de4fe5782ebe43aad485b49441075502 to your computer and use it in GitHub Desktop.
Save randhirraj3130/de4fe5782ebe43aad485b49441075502 to your computer and use it in GitHub Desktop.
how to rotate view in swift 3.0
// rotate imageview via 90 degree
myview.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
// rotate imageview via 180 degree
myview.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
// rotate imageview via 270 degree
myview.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)
// totate iamge
func imageRotatedByDegrees(oldImage: UIImage,degrees: CGFloat) -> UIImage {
let size = oldImage.size
UIGraphicsBeginImageContext(size)
let bitmap: CGContext = UIGraphicsGetCurrentContext()!
//Move the origin to the middle of the image so we will rotate and scale around the center.
bitmap.translateBy(x: size.width / 2, y: size.height / 2)
//Rotate the image context
bitmap.rotate(by: (degrees * CGFloat(M_PI / 180)))
//Now, draw the rotated/scaled image into the context
bitmap.scaleBy(x: 1.0, y: -1.0)
let origin = CGPoint(x: -size.width / 2, y: -size.width / 2)
bitmap.draw(oldImage.cgImage!, in: CGRect(origin: origin, size: size))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment