iosswiftuiimageviewuiimagecgaffinetransform

Rotate Image 5 degrees each time user clicks button in swift


I am creating a custom image editor for a project where I am stuck at rotating images by 5 degrees forward and backward. here is what I have tried so far

 1. self.cropImageView.transform =
    self.cropImageView.transform.rotated(by: CGFloat(Double(5) *
    .pi/180)) 
 2. self.cropImageView.transform =
    CGAffineTransform(rotationAngle: CGFloat(Double(5) * .pi/180) ) 

Here is the issue in 1 case image only rotates one time and in another case, the image rotates but the image size is getting smaller as I rotate it.

enter image description here


Solution

  • The code will only rotate the imageView,if the image size changes,you may have missed some code.

    func rotate(isForward: Bool) {
        self.cropImageView.transform = self.cropImageView.transform.rotated(by: (isForward ? -1 : 1)  * .pi * (5.0 / 180.0))
    }