1

How do I rotate an image in iPhone programming?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Raju
  • 3,459
  • 12
  • 37
  • 30
  • Dupe? http://stackoverflow.com/questions/917713/uiimage-rotation-custom-degrees – crashmstr Jun 05 '09 at 12:53
  • Might be a dupe, but depends on what the OP actually wants to do. Raju, what's the goal? Do you mean "get a copy of a UIImage that is rotated?" Or perhaps "display an image rotated?" Or perhaps "animate an image rotation?" This is a very vague questions. – Rob Napier Jun 05 '09 at 14:17
  • This might also be similar to what he's asking: http://stackoverflow.com/questions/839296/how-can-i-rotate-a-uiimageview-with-respect-to-any-point-other-than-its-center – Brad Larson Jun 05 '09 at 16:24

4 Answers4

2

A great rotation and image UIImage category by Allen Brunson can be found here: http://www.platinumball.net/blog/2010/01/31/iphone-uiimage-rotation-and-scaling/

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Ross
  • 14,266
  • 12
  • 60
  • 91
1
image.transform = CGAffineTransformMakeRotation(3.142);

rotates it 180 degrees.

miniman
  • 47
  • 1
  • 8
0
//If someone needs in swift 3 



    func rotateImageClockWise(theImage: UIImage,imageView:UIImageView) -> UIImage {

        var orient: UIImageOrientation!
        let imgOrientation = theImage.imageOrientation


        UIView.transition(with: imageView, duration: 0.2, options: .transitionCrossDissolve, animations: {() -> Void in

            switch imgOrientation {

            case .left:
                orient = .up

            case .right:
                orient = .down

            case .up:
                orient = .right

            case .down:
                orient = .left

            case .upMirrored:
                orient = .rightMirrored

            case .downMirrored:
                orient = .leftMirrored

            case .leftMirrored:
                orient = .upMirrored

            case .rightMirrored:
                orient = .downMirrored
            }


        }, completion: { _ in })

        let rotatedImage = UIImage(cgImage: theImage.cgImage!, scale: 1.0, orientation: orient)
        return rotatedImage
    }
//Just call the method like this :

rotateImageClockWise(theImage: UIImage,imageView:UIImageView)
Swifty Codes
  • 992
  • 10
  • 23
0

You can rotate a view using a CCGAffineTransform. You can create a rotation matrix by specifying the degrees in radians you want to rotate, then setting the rotation matrix to be the .transform property of your view.

Dan Lorenc
  • 5,376
  • 1
  • 23
  • 34