1

I have a PNG file in my Bundle.main and I want use that URL or path to feed my Image in SwiftUI, right now I am using this down code, but I like to use just SwiftUI not UIKit mixed

Image(uiImage: UIImage(contentsOfFile: path)!)

Do we have some initializing way for Image that accept path or url?

1 Answer 1

2

If you want to init from a filepath you could create an extension on Image that handles the boilerplate code for you. But you need to handle the case if the UIImage doesn't exist as the Image(uiImage:) expects a non-optional UIImage as a parameter.

Something like this should work:

extension Image {
    init(contentsOfFile: String) {
        // You could force unwrap here if you are 100% sure the image exists
        // but it is better to handle it gracefully
        if let image = UIImage(contentsOfFile: contentsOfFile) {
            self.init(uiImage: image)
        } else {
            // You need to handle the option if the image doesn't exist at the file path
            // let's just initialize with a SF Symbol as that will exist
            // you could pass a default name or otherwise if you like
            self.init(systemName: "xmark.octagon")
        }
    }
}

You would then use it like this:

Image(contentsOfFile: "path/to/image/here")

Or you could use the property of UIImage to load from the bundle

extension Image {
    init(uiImageNamed: String) {
        if let image = UIImage(named: uiImageNamed, in: .main, compatibleWith: nil) {
            self.init(uiImage: image)
        } else {
            // You need to handle the option if the image doesn't exist at the file path
            self.init(systemName: "xmark.octagon")
        }
    }
}

You would then use it like this:

Image(uiImageNamed: "ImageName")
Sign up to request clarification or add additional context in comments.

9 Comments

I have a kind of your extension as form of function which do the same thing, I was very interested to bundle, that you maintained before you edited your answer, I tried this code but it does not worked, what I am doing wrong here? Image("myImageName", bundle: Bundle(url: Bundle.main.url(forResource: "myImageName", withExtension: "png")!))
The code I removed doesn't work, that is why I removed it. Image appears to only look in asset catalogs, where as UIImage looks in the bundle as well as the asset catalogs. So you cannot initialise the way you are trying there. Also what you are doing seems like a roundabout way to write Bundle.main
I noticed as well what you just said, do you think this is a bug that it goes asset catalog for searching? and not looking to given Bundle?
because apple says: Parameters: name: the name of the image resource to lookup, as well as the localization key with which to label the image. bundle: the bundle to search for the image resource and localization content. If nil, uses the main Bundle. Defaults to nil.
It could be a bug, or it could be that Apple is expecting that all images should be contained in an asset catalog.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.