Posts

Showing posts with the label uiimage

UISegmentedControl maintain aspect ratio on image

Image
UISegmentedControl maintain aspect ratio on image I'm having a hard time making images on a UISegmentedControl keep their aspect ratio and not scale to all sides. Here's what it looks like: The images are 100X100(except nr 6 gonna replace soon) and I want them to maintain the aspect ratio. I have tried: self.segment.contentMode = .ScaleAspectFit //and: self.segment.contentMode = UIViewContentMode.Center Are there a way I can target the UIImageView in the segment..that way I can set the contentMode..right? Or is there another approach? Thanks 3 Answers 3 segmentedControl.subviews.flatMap{$0.subviews}.forEach { subview in if let imageView = subview as? UIImageView, let image = imageView.image, image.size.width > 5 { // The imageView which isn't separator imageView.contentMode = .scaleAspectFit } } Please add an explanation. ...

How to add background colour in existing image using Swift?

How to add background colour in existing image using Swift? I am trying to add background colour to existing image but it is not working. Here below is my code:- extension UIImage { func imageWithColor(tintColor: UIColor) -> UIImage { let rect = CGRect(x: 0, y: 0, width: 1, height: 1) UIGraphicsBeginImageContextWithOptions(rect.size, false, 0) tintColor.setFill() UIRectFill(rect) let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() //self.type(of: init)(ciImage: CIImage(image: image)!) return image } } extension UIImage { /** Returns an UIImage with a specified background color. - parameter color: The color of the background */ convenience init(withBackground color: UIColor) { let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1) UIGraphicsBeginImageContext(rect.size); let context:CGContext = UIGraphicsGetCurrentContext()!; context.setFillColor(color.cgColor); context.fill(rec...