How to change size of thumb image of UISlider programmatically

后端 未结 2 1552
春和景丽
春和景丽 2021-02-06 06:11

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

|o----------| -> |-----O------| -> |------------〇|

the thumbImage will be small at

相关标签:
2条回答
  • 2021-02-06 06:29

    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
        }
    }
    
    0 讨论(0)
  • 2021-02-06 06:36

    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 will have, will be a method A that will call the imageWithImage:scaledToSize: when the UISlider's value changes.

    0 讨论(0)
提交回复
热议问题