Reputation: 2970
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.
Upvotes: 1
Views: 2358
Reputation: 130183
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.
Upvotes: 1