Search code examples
iosswiftswift3uiimageviewuislider

How to change specified image size in UIImageView with UISlider in Swift programmatically


I'm trying to use the UISlider to change specified image size in UIImageView. The value range of the slider is 0 ~ 100. So the idea is that if I move the slider smaller, a specified image shows smaller, and If I move the slider bigger, the image size getting bigger.

This is the case when the image getting smaller: getting smaller image by UISlider

And this is the case when the image getting bigger: getting bigger image by UISlider

Can someone please help?


Solution

  • Make the slider's value range from .01 to 1.0 (.01 should work better than 0.)

    Control-drag from your slider into your view controller and create an action on the slider's valueChanged event. Let's call it sliderChanged.

    In the slider's action method, change the scale on the image view:

    @IBOutlet myImageView: UIImageView*
    
    
    @IBAction func sliderChanged(sender: UISlider) {
      let scale = sender.value
      let transform = CGAffineTransform.scale(scaleX: scale, y: scale)
      myImageView.transform = transform
    }
    

    That should do it.