Xcode 11: Failed to instantiate image from asset catalog linked with a custom bundle on iOS 12

When I add a bundle target to my project and link the image with a new asset catalog to bundle,

It is failed to instantiate UIImage from the asset catalog in the bundle that I created in iOS 12 or below (iOS 13 works fine).

This error occurred only in Xcode 11


In short,


Build to iOS 13 in Xcode 11: Successfully image loaded.

Build to iOS 12 or iOS 11 in Xcode 11: Failed to load the image.

Build to iOS 12 or iOS 11 in Xcode 10.3: Successfully image loaded.


I've attached the sample app to simulate: https://github.com/momamene/AssetCatalogImageSampleApp

The core code is shown below.


import UIKit

class CustomBundleViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let resourcePath = Bundle.main.path(
            forResource: "CustomBundle",
            ofType: "bundle"
        )!
        let bundle = Bundle(path: resourcePath)!
        // NOTE: image is nil on iOS 12 and iOS 11
        let image = UIImage(named: "sample-image", in: bundle, compatibleWith: nil)
        let frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        let imageView = UIImageView(frame: frame)
        imageView.image = image
        view.addSubview(imageView)
    }
}


I think this is absolutely a bug of Xcode 11.

Replies

I just fought through a similar problem (image that failed to load was in an asset catalog in an included Framework).


Eventually I stumbled upon the Build Setting, Asset Catalog Compiler - Options, Build Active Resources Only. When I changed that value to NO in my framework and rebuilt it, the image loads as expected on iOS 12 & 11.

Just a check: did you perform an option-clean build folder ?

Have you tried closing and re-opening Xcode?
Restarting Xcode solved this issue for me.