swiftsvguibuttonuiimageviewuikit

Swift - SVG image loses quality when resized


img1

As you can see on the image above, these two svg images are losing their quality a bit. In Assets's Attributes inscpector, their Scale property is set to Single Scale.
Those are button's imageViews, resized to fill the button:

button.setImage(UIImage(named: img1.rawValue), for: .normal) 
button.contentHorizontalAlignment = .fill
button.contentVerticalAlignment = .fill
button.imageView?.contentMode = .scaleAspectFit

Solution

  • I'm using this piece of code for resizing images to the desired size without losing quality

    extension UIImage {
        func resize(targetSize: CGSize) -> UIImage {
            return UIGraphicsImageRenderer(size:targetSize).image { _ in
                self.draw(in: CGRect(origin: .zero, size: targetSize))
            }
        }
    }