How to use image contained in Resource framework from main module?

1.2k Views Asked by At

I made embed framework Resources.framework to encapsulate Resources like images and colors. When I put image resources to main module's storyboard, InterfaceBuilder present all images correctly, But compiled application didn't present Images. Why? Color resources are presented. Images only. Can I use image resources from embed frameworks?

1

There are 1 best solutions below

0
Andrew Fritz On BEST ANSWER

As of Xcode 12.1, I do not believe there is a way to do this.

The reason is that when you configure an images using storyboards Xcode defaults to assuming those images are located in the bundle for the current module, and the Attributes inspector does not provide a way to specify an alternate module, as it does in the case of view controller classes:

View controller attribute inspector with Module property

But in code this is easy to accomplish using the in:Bundle? parameter for initializing a UIImage, which tells the app to look in a specific bundle for the image resource at runtime.

guard
    let bundleURL = Bundle.main.url(forResource: "[bundle filename without extension]", withExtension: "bundle"),
    let bundle = Bundle(url: bundleURL) else { return }
        
let image = UIImage.init(named: "[image name]", in: bundle, compatibleWith: nil)

It is likely an oversight on Apple's part to allow Xcode to show image resources from other modules in the image name field, but not properly link them when compiling storyboards. I hope they fix this.

Beware of using color assets from other modules as well:

While they appear to work, it is only because when you assign color assets in a storyboard, Xcode actually encodes the color information from the asset in the storyboard file itself. One sign this is not intended to work is that if you define a Color Set asset with a variation for Dark Appearance, only the Any Appearance color will be used (even in dark mode). Color Set assets in the same module, however, work normally.

Color Set Asset with Any Appearance and Dark Appearance