11

I am currently updating a project to use image literals, to enjoy the benefits of non-optional images. The project is importing various frameworks, and the frameworks are containing images.

In the frameworks, we had to declare an extension on UIImage to override the initialiser, because it was looking for the image in the wrong bundle. We did something like:

extension UIImage {   
    convenience init?(framework_named imageName: String) {
        let bundle = Bundle(for: ClassNameInFramework.self)
        self.init(named: imageName, in: bundle, compatibleWith: nil)
    }
}

I am wanting to use image literals in the framework too, but like before, the literals are looking for the wrong bundle for the image and the application crashes.

Does anyone know, how to specify the image literal to which bundle to look for the image?

2
  • 1
    Can't specify unfortunately. The image literals call the init(named:) initializer which looks in the main bundle.
    – dan
    Jan 24, 2017 at 18:31
  • 2
    @dan could you might point me to some documentation?
    – dirtydanee
    Jan 25, 2017 at 23:04

1 Answer 1

29

I discovered a pretty simple workaround and was amazed to not find it anywhere on other posts. I wish it could be more elegant, but it's preferable than using the stringly-typed UIImage(named:in:compatibleWith:) initializer in my opinion.

We basically take advantage of the _ExpressibleByImageLiteral protocol, which is what Xcode uses to determine if a type is expressible by an image literal. It's part of the Swift standard library, but it's hidden from autocompletion, I guess because it's uncommon to want to initialize a custom type with an image literal. However, it's just what we want and behaves exactly like the other ExpressibleBy protocols.

struct WrappedBundleImage: _ExpressibleByImageLiteral {
    let image: UIImage?

    init(imageLiteralResourceName name: String) {
        let bundle = Bundle(for: ClassInFramework.self)
        image = UIImage(named: name, in: bundle, compatibleWith: nil)
    }
}

Note that I use a wrapper instead of a UIImage subclass, which would seem like a better option. Sadly, classes like UIImage are not intended to be subclassed and you will find yourself getting a lot of headaches if you do.

And now its usage becomes:

let image = (🏞 as WrappedBundleImage).image

Not as concise as a normal image literal, but it's the best we've got for now. We just have to remember to do the as casting, otherwise our custom initializer will not get called.

You can also do something like this:

extension UIImage {
    static func fromWrappedBundleImage(_ wrappedImage: WrappedBundleImage) -> UIImage? {
        return wrappedImage.image
    }
}

And we can now use it like this:

UIImage.fromWrappedBundleImage(🏞)

Hope this helps!

4
  • Nice solution. The only thing that could be an issue is the fact that the default image literals is still visible in the "Media Library" list. This is the list on the bottom right in Xcode. This could result in issues when people use literals from this list because these literals still point to the main Bundle.
    – bartosss
    Apr 11, 2018 at 20:20
  • You're right @bartosss, thanks for the comment. I've updated my answer above to use optionals instead, in case we do provide a literal from the main bundle by mistake. Jul 18, 2018 at 2:09
  • 3
    @diegomontoyas Isn't _ExpressibleByImageLiteral technically a private API? Dec 31, 2018 at 21:26
  • It is a public protocol github.com/apple/swift/blob/master/stdlib/public/core/…
    – jusynth
    Aug 5, 2020 at 16:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.