1

I want to use a UISlider to control the size of an image. As you slide the slider one way the UIImage above should grow and as you slide the other way it should shrink. The ratio should stay the same. The math should be something like slider value = width, and height = (height/width ratio) * slider value. The way I think this could be done would be by making a variable and setting that variable to the image size and then making that variable = the slider value but I am not experienced with this sort of programming and do not know where to start. I found this question but I am not sure how to implement its code. Any point in the right direction, even just a conceptual explanation so I know how to write the code, is appreciated. NOTE- I did find this answer on changing the size, so now I just need to figure out how to link that to a slider.

EDIT- Now that the code is done and working, if anyone want s to see it in action my repo is here.

Community
  • 1
  • 1
Max von Hippel
  • 2,856
  • 3
  • 29
  • 46

1 Answers1

1

I know you specified UIImage in your question, but if you're trying to adjust the size of an image displayed on screen, you would do this by modifying the image view that contains the image. In this case you would want to modify the transform property of the image view.

Start by setting the sliders min value to 0.5 and max to 1.5, and linking its value changed control event to an IBAction set up to look something like this. It will expand and contract an image view from the image view's center.

- (IBAction)sliderValueChanged:(UISlider *)sender
{
    [_myImageView setTransform:CGAffineTransformMakeScale(sender.value, sender.value)];
}

Additionally, the first post your linked to if for Microsoft WPF and the code couldn't be directly converted without knowing the both relevant API's. Then the second link you provided does show how to directly change the resolution of a UIImage.

You could do something similar to what I've done above with the image view, as in reading the sliders value and formulaically setting the image's size, but I don't recommend that. The slider value changed method will be called very frequently, and may cause performance issues doing this. If you provide more details on what you're trying to do, I may be able to offer different suggestions.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • I am trying to make a rangefinder style camera app where you change the size of the framelines and thus can zoom while taking pictures without obscuring your field of view. Once I set up the slider to control the uiimageview size I will make a button that screenshots the image, which I already know how to do. I hope it is high enough resolution to work alright. Anyways your code and explanation look pretty clear and I will start trying to implement them ASAP and will accept your answer as soon as I either get it working or come to the conclusion that you are definitely correct. – Max von Hippel Sep 01 '13 at 02:56
  • Thanks, also one question- with your code my image gets bigger from the top left corner outwards. Is there a way to make it stay centered on the view so that while it may resize itself its center does not move? I can post a screenshot of what's happening if that helps. – Max von Hippel Sep 01 '13 at 03:31
  • Actually it turns out that the problem was that I had autolayout turned on, I turned that off and now it works beautifully. Much appreciated! – Max von Hippel Sep 01 '13 at 03:44