Wednesday, October 28, 2026
HomeiOS Developmentios - UIImage(named:) with interval hundreds incorrect picture (e.g. for SFSymbols)

ios – UIImage(named:) with interval hundreds incorrect picture (e.g. for SFSymbols)

[ad_1]

I need a straightforward strategy to load SFSymbol photographs with configuration, if doable. Else simply load the picture usually. So I’ve this extension and I’ve all SFSymbols I want in in my assetcatalog with the identical identify because the image (on this case I’ve “circle” and “circle.fill“).

Nevertheless, in iOS 12 this offers me “circle“!

I assume what’s occurring is that it counts the dot/interval (i.e. circle . fill) as a file extension and assumes the extension is incorrect so simply seize the circle picture with the ‘different extension’. Even tho it isn’t. Is there a straightforward repair for this? Is that this supposed?

class ViewController: UIViewController {
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        let imageView = UIImageView(body: CGRect(x: 30, y: 30, width: 50, top: 50))
        imageView.picture = UIImage.picture(sfsymbolName: "circle.fill")
        view.addSubview(imageView)
    }
}

extension UIImage {
    static func picture(sfsymbolName: String, config: Any? = nil) -> UIImage? {
        if #obtainable(iOS 13.0, *), let sfImage = UIImage(systemName: sfsymbolName, withConfiguration: config as? UIImage.Configuration) {
            return sfImage
        } else {
            return UIImage(named: sfsymbolName)
        }
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments