stackich
stackich

Reputation: 5237

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

Upvotes: 5

Views: 2198

Answers (1)

Alastar
Alastar

Reputation: 1374

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))
        }
    }
}

Upvotes: 2

Related Questions