How to use images asset catalog in cocoapod library for iOS

前端 未结 11 2365
粉色の甜心
粉色の甜心 2020-12-15 16:38

I have a cocoapod library which contains assets in 2 formats:

  • a .storyboard
  • XCode asset catalog .xcassets (with images)

my podsp

11条回答
  •  再見小時候
    2020-12-15 17:24

    If you're using SwiftUI, using the Image.init(_ name: String, bundle: Bundle?) initializer won't work, you have to initialize it like this:

     func getImage(named name: String) -> UIImage {
        // Use a random singleton class in your project.
        // Emphasis on class, structs won't work.
        let bundle = Bundle(for: [random class].self)
    
        if let image = UIImage(named: name, in: bundle, compatibleWith: nil) {
            return image
        }
    
        return .init()
    }
    
    Image(uiImage: getImage(named: "image_name"))
    

提交回复
热议问题