How to resize thumb image of UISlider programmatically

I would like to make a custom UISlider, something like this

| about ---------- | → | ----- O ------ | → | ------------ 〇 |

thumbImage will be minimal at the minimum value, it will increase in size when the value of the slider increases, otherwise it will decrease.

Does anyone know how to do this?

+6
source share
2 answers

You can use this code:

 + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { //UIGraphicsBeginImageContext(newSize); UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

Taken from here .

The extra work you'll have will be method A , which will call imageWithImage:scaledToSize: when changing the value of UISlider's .

+10
source

Swift 3:

 extension UIImage { func scaleToSize(newSize: CGSize) -> UIImage { UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext(); return newImage } } 
+2
source

Source: https://habr.com/ru/post/919104/


All Articles