Load image from CocoaPods resource bundle

后端 未结 7 2085
独厮守ぢ
独厮守ぢ 2021-02-02 11:48

I am creating a private library that includes resources that are referenced by components in the library. The library is shared with apps using CocoaPods. In the .podspec for

7条回答
  •  梦毁少年i
    2021-02-02 12:37

    This answer works with Swift3 and Swift4.

    Create a function under the pod directory into the file.

    func loadImageBundle(named imageName:String) -> UIImage? {
        let podBundle = Bundle(for: self.classForCoder)
        if let bundleURL = podBundle.url(forResource: "Update with directory name", withExtension: "bundle")
        {
            let imageBundel = Bundle(url:bundleURL )
            let image = UIImage(named: imageName, in: imageBundel, compatibleWith: nil)
            return image
        }
        return nil
    }
    

    Usage

    imageView.image = loadImageBundle(named: "imagefile")
    

提交回复
热议问题