iosswiftimageviewscaleuipinchgesturerecognizer

Get original image size after UIPinchGestureRecognizer pinch in and out


I would like to return the original image size after I use pinch gesture to zoom in or out.

What I have found the similar answer is

How to get original image when we zoom in and zoom out the image in ios,

it uses scrollView. Not exactly what I need.

Here is my code, It's a gesture response sent by #selector

func respondToGesture(_ gesture: UIGestureRecognizer) -> Void {

    if let pinch = gesture as? UIPinchGestureRecognizer {

        if let img = pinch.view as? UIImageView {

           img.transform = CGAffineTransform(scaleX: pinch.scale, y: pinch.scale)

           if pinch.state == .ended
           {
               img.transform = CGAffineTransform(scaleX: (1 / pinch.scale), y: (1 / pinch.scale))
           }
        }
    }
}

Hope to get some suggestions!! Thanks~


Solution

  • Use this to reset the size of your image

    yourImage.transform = CGAffineTransform.identity
    

    Hope this helps!