How to rotate an image 90 degrees on iOS?

user1511244 picture user1511244 · Jul 26, 2012 · Viewed 84.5k times · Source

What I want to do is take a snapshot from my camera , send it to a server and then the server sends me back the image on a viewController. If the image is in portrait mode the image appears well on screen , however if the image was taken in landscape mode the image appears streched on the screen(as it tries to appear on portrait mode!). I dont know how to fix this but i guess one solution is first to check if the image is in portrait/landscape mode and then if it is on landscape mode rotate it by 90degrees before showing it on screen. So how could i do that?

Answer

mandeep-dhiman picture mandeep-dhiman · Jul 26, 2012
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);

Swift 4+:

self.imageview.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))