How to Rotate a UIImage 90 degrees?

前端 未结 19 1237
天涯浪人
天涯浪人 2020-11-22 08:45

I have a UIImage that is UIImageOrientationUp (portrait) that I would like to rotate counter-clockwise by 90 degrees (to landscape). I don\'t want

19条回答
  •  误落风尘
    2020-11-22 09:16

    If you want to add a photo rotate button that'll keep rotating the photo in 90 degree increments, here you go. (finalImage is a UIImage that's already been created elsewhere.)

    - (void)rotatePhoto {
        UIImage *rotatedImage;
    
        if (finalImage.imageOrientation == UIImageOrientationRight)
            rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage
                                                  scale: 1.0
                                            orientation: UIImageOrientationDown];
        else if (finalImage.imageOrientation == UIImageOrientationDown)
            rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage
                                                  scale: 1.0
                                            orientation: UIImageOrientationLeft];
        else if (finalImage.imageOrientation == UIImageOrientationLeft)
            rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage
                                                  scale: 1.0
                                            orientation: UIImageOrientationUp];
        else
            rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage
                                                         scale: 1.0
                                                   orientation: UIImageOrientationRight];
        finalImage = rotatedImage;
    }
    

提交回复
热议问题