How can I migrate an xcassets catalog to Swift Package Manager?

Viewed 4783

I am migrating an app to Swift Package Manager. All my images are in an .xcassets, with their configurations for different display sizes etc.

How can I migrate those assets to SPM?

1 Answers

Starting with XCode 12 and Swift 5.3, it is possible to include images in Swift Package. I had to do a few modifications:

  • Add // swift-tools-version:5.3 as the header of the Swift package file
  • Simply create a new asset catalog, or copy an existing one to the package hierarchy. .assets folder get compiled automatically.enter image description here
  • Make the image available in external targets by resolving the correct bundle. I went for this in the target containing the assets and an alternative would be to make each public asset available as a specific value.:
public func SCImage(named name: String) -> UIImage? {
  UIImage(named: name, in: Bundle.module, compatibleWith: nil)
}

I can now do

import SCImages

let image = SCImage(named: "foo")
Related