How to use images asset catalog in cocoapod library for iOS

前端 未结 11 2321
粉色の甜心
粉色の甜心 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:20

    I can't still get it to work, with none of the solutions above. I have an Assets.xcassets file in my Pod.

    And inside my Pod classes i would like to access images from the Assets

    Specified like this:

    s.resource_bundles = {
         'SWDownloadPlayButton' => ['SWDownloadPlayButton/Assets/Assets.xcassets']
    }
    

    With this helper:

    public struct ImagesHelper {
        private static var podsBundle: Bundle {
            let bundle = Bundle(for: SWDownloadPlayButton.self)
            return Bundle(url: bundle.url(forResource: "SWDownloadPlayButton",
                                          withExtension: "bundle")!)!
        }
    
        private static func imageFor(name imageName: String) -> UIImage {
            return UIImage.init(named: imageName, in: podsBundle, compatibleWith: nil)!
        }
    
        public static var download_icon: UIImage {
            return imageFor(name: "download_icon")
        }
    }
    

    But whatever i do inside my Pod classes (not in the example project)...like this

     var centerImage: UIImage = ImagesHelper.download_icon.withRenderingMode(.alwaysTemplate) {
            didSet {
                updateImage()
            }
        }
    

    I still get a nil on the centerImage

提交回复
热议问题