Load image from CocoaPods resource bundle

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 00:09:50

This turns out to be a problem with asset catalogs, not CocoaPods or bundles. Moving the images out of the asset catalog solved the problem. It looks like Apple doesn't support asset catalogs in secondary resource bundles. Pity.

For example Paramount

podspec

s.resource_bundle = {
    'Paramount' => ['Sources/Paramount.bundle/*.png']
}

swift

public extension UIImage {
  static func make(name: String) -> UIImage? {
    let bundle = NSBundle(forClass: Paramount.Toolbar.self)
    return UIImage(named: "Paramount.bundle/\(name)", inBundle: bundle, compatibleWithTraitCollection: nil)
  }
}

Here Paramount.Toolbar.self can be any class in that framework

The obvious solution that wasn’t listed anywhere is using the Cocoapods generated bundle (which the folder actually doesn’t exist) as a NSBundle:

have a try: http://blog.flaviocaetano.com/post/cocoapods-and-resource_bundles/

it worked for me!!

As for me, I had to add the "s.resources" into the .podspec. Before this was missing.

Only the s.resource_bundles were set this way: 'BPPicker/Assets/.', changed it to 'BPPicker/Assets/*' as well.

#
# Be sure to run `pod lib lint BPPicker.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'BPPicker'
  s.version          = '0.1.11'
  s.summary          = 'This is the imito Body Part Picker.'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://bitbucket.org/imito/bppicker'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Ievgen Naloiko' => 'naloiko@gmail.com' }
  s.source           = { :git => 'https://ievgen_naloiko@bitbucket.org/imito/bppicker.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '9.0'

  s.source_files = 'BPPicker/Classes/**/*'

  s.resource_bundles = {
    'BPPicker' => ['BPPicker/Assets/*']
  }
    s.resources = "BPPicker/**/*.{png,json}"

    s.frameworks = 'UIKit'
    s.dependency 'ObjectMapper'
end

I made a pod that can locate the resource bundles in pods. I hope it will end the "CocoaPods resource bundle" problem once and for all.

https://github.com/haifengkao/PodAsset

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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!